Daniel
Daniel

Reputation: 301

What is the Objective-C equivalent to main()?

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:

  1. there is .h file which contains the declaration for every class; like we have in C++ where we can declare the name of the variables/data_fields and later define the functions/methods outside
  2. *The functions/methods are declared in a .m file so for every class there will be a .h file, a .m file and a.xib file

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

Answers (3)

Aran Mulholland
Aran Mulholland

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

TechZen
TechZen

Reputation: 64428

I think iPhone development presents two challenges for programmers experienced in other languages/APIs:

  1. The API does so much for you that it is difficult to get a grasp for how the program is actually structured. People used to starting apps from scratch keeping expecting to have to do more a lot more work to get an app launched.
  2. The Interface-Builder/nib technology hides a lot of complexity that experienced programmers are used to dealing with. It seems like views, controllers etc just pop out from nowhere into classes.

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

Mark Bessey
Mark Bessey

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

Related Questions