views:

82

answers:

3

In an XCode project that has a lot of h and m files, how do you find out which file gets executed first?

+9  A: 

The file that contains int main(int argc, char * argv[]); will get run first, since the main() function is the first function to get run. In pretty much every Xcode template project, that file is called "main.m".

edit

Usually, the main() function of a Cocoa Touch app is this:

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

(Substitute NSApplicationMain() for UIApplicationMain() and remove the autorelease pool if you're writing a Mac app)

edit #2

I'm only interested in the file that gets run first from the classes folder

The simple answer is "The application delegate", then everything else.

The technical answer to that is that any objects in your MainMenu.xib (Mac) or MainWindow.xib (iOS) file will be instantiated first. Usually the objects in that file will be instantiated in the order in which they appear, but I don't think that's guaranteed.

So if you 3 have custom top-level objects in your MainWindow.xib file, then they'll all be instantiated at the same time (within reason). First their initWithCoder: methods will execute, then some time later their awakeFromNib methods will execute (which usually the safest "starting point" for that object).

The application delegate launch methods will happen somewhere along in there too (I believe in between initWithCoder: and awakeFromNib).

Dave DeLong
Correct, but... it is rare that you should be adding code to main() and almost always the case that there is a more appropriate hook for your initialization code somewhere else in the project.
bbum
@bbum agreed, unless you're writing a Foundation tool.
Dave DeLong
I don't see a main.m file in the classes folder.
awakeFromNib
@awakeFromNib look in "Other Sources"
Dave DeLong
I'm only interested in the file that gets run first from the classes folder.
awakeFromNib
@awakeFromNib edited answer
Dave DeLong
Dave DeLong: No, that's the `main` function for a Cocoa Touch app. The `main` function for a Cocoa app is simply `return NSApplicationMain(argc, argv);`.
Peter Hosey
@Peter is the autorelease pool really that big of a deal? :P (edited answer...)
Dave DeLong
awakeFromNib: Classes are not run; functions the program may call other functions/methods in between.) Beyond that, the order in which methods run is entirely up to the program. Start where Dave told you to, and read from there.
Peter Hosey
Dave DeLong: My point, reflected in your latest edit (thanks), is that Cocoa is not Cocoa Touch. UIApplicationMain is not a Cocoa function; it is a Cocoa Touch function. Likewise, NSApplicationMain is Cocoa, not Cocoa Touch. I didn't care about the autorelease pool. ☺
Peter Hosey
@Peter got it. point well taken.
Dave DeLong
+1  A: 

Another thing that should get run first is in an App Delegate (Defined like so: NSObject <UIApplicationDelegate> and setup in the nib) the method applicationDidFinishLaunching

However this is obviously not technically the first thing to be run. Things like code that loads nibs is executed first but this is the first thing to get called in general where you have control.

You can also create a class called like "Controller" and drag an NSObject into your nib and set the class to Controller. Then the method awakeFromNib will be called.

Either of these should be fine to set up your app.

Justin Meiners
I'm not trying to setup an app, I'm trying to figure out an app that someone else made.
awakeFromNib
Sorry, I thought you were because of this comment "more appropriate hook for your initialization code somewhere else in the project."
Justin Meiners
+2  A: 

Cocoa and Cocoa-Touch apps are completely event-driven. It is not that the order the methods are executed can be understood by reading the source code files.

  • As Dave explained, the entry of the program is at the main function in main.m. It immediately calls UI/NSApplicationMain.
  • NS/UIApplicationMain is a function provided by Cocoa(-Touch). It watches the user interaction, and fires events accordingly.
    • For example, when the user clicks a button, the system calls automatically what you provided as the action method specified in the xib file.
    • Another example is the drawRect: method you provide: it's called when the system decides to draw an object onto the screen. It's very important that you don't actively draw to the screen. The system asks you to draw the screen, and you respond.
    • One important set of events are the ones which are called at the beginning of the program, like applicationDidFinishLaunching: or the ones which are called when a view is loaded from the xib file, viewDidLoad.
  • Aaron Hillegass made a great analogy of the situation: you're KITT and respond to Michael Knight's order, not the other way around. Well you need to be over certain age to understand this analogy...
Yuji