I am new to iPhone development and Objective C. I have decided I would do some tutorials on Objective C first to get the hang of the language, however there a few things I am unsure about and would be grateful if someone could explain them to me.
I have downloaded Xcode 4.2 from app store, so am using.
First question, can you only have one main class in objective C? I am going through the NewBoston tutorials at the moment, and he has many different tutorials, and I am used to Java programming, where I just create a new class for each tutorial. In Objective C however, when you create a new project, you get a file called main.m which seems to be default class which always executes. I am used to java, where when you just create one project, and can have as many classes as you want within it and can just choose which class you want to run. Can you not do something like this in Objective C? Or do I have to create a new project for each tutorial I do? As I don’t want to keep just changing the main class, when I finish a tutorial I like to keep the file so I can go back and check on it if I ever forget anything.
And second quick question about the main method in Objective C. The main method is declared like this
int main(int argc, char *argV[]){
Why is the main method an int declaration? I am used to Java, where main method is void as main methods don’t return anything. It seems odd to me that the main method for Objective C is an int. Just would be nice to understand.
Thanks in advance for any help
1) In Objective-C you have one
mainfunction that gets run at startup. Combined with the fact that the iOS runtime does some initialization, too, and get that information from the project info (e.g. startup Storyboard / NIB) you can only have one entry point per project. This somewhat stems from the fact that while Java maintains a lot of namespace info and has no predifined entry point per se, Objective-C does compile everything down to one file and looks for the one function calledmain. You could see this like a jar-file which has a predefined entry point set on its own; only you cannot change the name of the entry point.2) The return type of main: For a long time, programs had return codes to indicate success (return code 0) or failure (return code greater than zero, mostly documented in man pages etc.). Actually you do have the same in Java, have a look at
System.exit(int exitcode), Java simply has some default-handling as in man cases return codes are not needed anymore (especially when writing GUI applications).