Reputation: 301
I'm just a beginner at Objective-C and its syntax is just knocking me out of my mind.
I'm 'trying' to work on iphone 3.0.
Up till know I have learned that:
So how do we call the functions/methods of our choice?
In good old language format of C,C++,JAVA,C# we have a main() function which does our control work but what's the equivalent of it main() here in Obj-c?
I know there is a main() function too but I hardly know how it works.
Upvotes: 2
Views: 322
Reputation: 23935
The main function starts the main program event loop, generally you don't touch it.
the AppDelegate is where you want to put your own user code. If you generate a sample iPhone project called Sample you will generate a class called SampleAppDelegate, it has a method called - (void)applicationDidFinishLaunching:(UIApplication *)application
which is the entry point I think you are looking for.
The SampleAppDelegate class implements a delegate (like an interface in c#) from the UIApplicationDelegateProtocol some of the methods are optional applicationDidFinishLaunching is not. This is generally where you set up your first view controller to do your inital screen.
Calling a method is done via message passing. If I have a class Tom with a method print I will initialize and call the method as so
Tom *tom = [[Tom alloc]init];
[tom print];
oh no where did my method arguments go ? I feel lost without brackets.
If I have a method that prints page numbers and returns void I might define it as such
-(void)printPageNumbers:(int)pageNumber{
}
and call it like this
Tom *tom = [[Tom alloc]init];
[tom printPageNumbers:2];
multiple parameters
-(void)printPageNumbersFrom:(int)fromPageNumber toPageNumber:(int)toPageNumber{
}
and call it
Tom *tom = [[Tom alloc]init];
[tom printPageNumbersFrom:2 toPageNumber:5];
It's not a very type safe language, you can do some funky stuff like if you had an array of Tom objects, you could just send one of them a message. If at runtime it turned out that the object in the array wasn't a Tom you would get an exception.
[[myArray objectAtIndex:0] printPageNumbersFrom:2 toPageNumber:5];
Some comments on the above "so for every class there will be a .h file .m file .xib file" - this is incorrect. Each class has a '.h' and a '.m'.
A '.xib' is a view file, if your class has no ui element it won't have a '.xib'. the '.xib' called a nib file is not part of the class anyway, it just refers to it. (you link the two)
I found the learning curve fairly steep. Objective-C is not a hard language if you have C and some OO backing. However putting it all together with the ui can be a bit of a pain. Stanford uni has an online course which they have distributed through iTunes U I've watched them all they are worth the time, see here
enjoy the curve, I'm glad I'm not sitting where you are :)
Upvotes: 10
Reputation: 64428
I think iPhone development presents two challenges for programmers experienced in other languages/APIs:
As a result, experienced programmers always feel like they've missed something in learning the API because they expect the complexity that is hidden.
Upvotes: 1
Reputation: 19782
You really should work your way through the introductory documentation on Apple's developer website first. It is sometimes very helpful to work through things in a systematic matter when you're a beginner: Learning Objective-C: A Primer and Your First iPhone Application
Upvotes: 3