Let’s say I just created a Cocoa application. I have three files: AppDelegate.h, AppDelegate.m, and MainMenu.xib.
If I don’t touch anything and run the app, does the compiler actually compile AppDelegate.h and AppDelegate.m even though I haven’t written anything?
How does the compiler know what files to include?
Your question is very general but I’ll sketch a few of the high level details. When you create a new project in Xcode you choose a project template to base it on. In your case it sounds like you used the simple Cocoa Application template. This gives you AppDelegate.h, AppDelegate.h, and MainMenu.xib along with a few other files listed under the “Supporting Files” folder in the Project navigator. We’ll get to some of them later. First note that at the top level of your new project directory is something that looks like a file called something like MyProject.xcodeproj. This is actually directory packaged together to look like a single file and inside of it is a file named project.pbxproj. This file is an XML file that contains all the settings for your build. It comes preconfigured by the project template to build a simple application. The basic build process specified in the default project.pbxproj is:
If you look at main.m you’ll notice it looks a lot like an old style main.c except it runs
NSApplicationMain(). This function ends up creating an instance of the “Principal class” listed in the Info.plist in your project. By default this isNSApplication, which is a standard class provided by Cocoa. The “Main nib file base name” is then loaded, which is MainMenu.xib for your project. A nib file is basically just a bunch of “freeze dried objects” that are instantiated and connections are made between them. One of the objects in the MainMenu.xib file is an instance ofAppDelegate, which is how this code gets loaded into memory. In the default template theAppDelegatedoes nothing and MainMenu.xib contains just a standard application menu bar and a blank window. This is what you will see when you run the application.You get all this for free as a starting point before you write one line of code. At this point a Cocoa coder would typically start to fill out the
AppDelegate, write new classes, or add new nib files for other parts of the UI.