The Starting point
In C programming language (or languages derived from C), the programs start at a method called main().
Code Listing 1. The main() method in Objective-C programs
//
// main.m
// FirstApp
//
// Created by Infinite Zest on 9/11/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Figure 1. The annotated main method.

Now let’s discuss various parts of the main() method.
The import
One header file is imported for the main() method in iPhone applications - UIKit.h.
Code Listing 2. Importing UIKit
#import is a preprocessor directive; and is similar to #include. The difference is that with #import, files are not included more than once.
UIKit Framework
UIKit.h is part of the UIKit framework, which contains the classes necessary to build user interface for the iPhone / iPod touch application. This framework is in the file UIKit.framework and gets installed when the iPhone SDK is installed.
If you installed the iPhone SDK in the default location, you will find those SDK files in the following location:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
Here, you might find both iPhoneOS2.0.sdk and iPhoneOS2.1.sdk. The actual framework files would be in the following location:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/Frameworks
Figure 2. The UIKit.framework from Xcode. The header files that are part of the framework are shown on the left. The contents of UIKit.h file are shown on the right.

UIKit Header File
As you would imagine, the UIKit framework will define the individual user interface elements in their own separate classes. Each class is declared in its own header file. These user interface elements include the common UI elements like Button, Label, Text View, Date Picker, Font, Progress View, Scroll View, Tab Bar, Image, etc. You would also find advanced classes like Accelerometer, Web View, Window, View Controller, etc.
The header file UIKit.h conveniently imports the header files for all those classes.
Code Listing 3. The header file UIKit.h. As you can see the header files for UI element classes are here: Accelerometer, Alert, Application, Button, Color, Date Picker, Scroll View, Slider, and so on.
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIAccelerometer.h>
#import <UIKit/UIActivityIndicatorView.h>
#import <UIKit/UIAlert.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIBarItem.h>
#import <UIKit/UIBarButtonItem.h>
#import <UIKit/UIButton.h>
#import <UIKit/UIColor.h>
#import <UIKit/UIControl.h>
#import <UIKit/UIDatePicker.h>
#import <UIKit/UIDevice.h>
#import <UIKit/UIEvent.h>
#import <UIKit/UIFont.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIGraphics.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UIImagePickerController.h>
#import <UIKit/UIInterface.h>
#import <UIKit/UILabel.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavigationController.h>
#import <UIKit/UINibDeclarations.h>
#import <UIKit/UINibLoading.h>
#import <UIKit/UIPageControl.h>
#import <UIKit/UIPickerView.h>
#import <UIKit/UIProgressView.h>
#import <UIKit/UIResponder.h>
#import <UIKit/UIScreen.h>
#import <UIKit/UIScrollView.h>
#import <UIKit/UISearchBar.h>
#import <UIKit/UISegmentedControl.h>
#import <UIKit/UISlider.h>
#import <UIKit/UIStringDrawing.h>
#import <UIKit/UISwitch.h>
#import <UIKit/UITabBar.h>
#import <UIKit/UITabBarItem.h>
#import <UIKit/UITableView.h>
#import <UIKit/UITableViewCell.h>
#import <UIKit/UITableViewController.h>
#import <UIKit/UITextField.h>
#import <UIKit/UITextInputTraits.h>
#import <UIKit/UITextView.h>
#import <UIKit/UIToolbar.h>
#import <UIKit/UITabBarController.h>
#import <UIKit/UITouch.h>
#import <UIKit/UIView.h>
#import <UIKit/UIViewController.h>
#import <UIKit/UIWebView.h>
#import <UIKit/UIWindow.h>
Need for UIKit.h in main.m
The main() method itself is not using all these user interface classes. It uses one particular method: UIApplicationMain(), which is defined in the UIApplication.h file. This UIApplication.h is included in the UIKit.h above. The UIApplicationMain() method is described below.
The main() signature
Following is the signature of the method main:
Code Listing 4. Signature of the main() method
int main(int argc, char *argv[]) {
}
The arguments
This is the typical set of arguments for the main method in C programs (or Objective-C programs). Couple of other variations exist: no arguments at all, and sending a list of environment variables in addition to the above two arguments.
The parameter argc stands for argument count. This is the number of arguments sent to this program in the next array. argc would be at least 1, as the first string in argv is the name of the program.
The parameter argv[] is an array of strings (or array of pointers to characters, as the declaration char * says). The array index starts from 0; hence argv[0] will always be the name of the program.
In a simple first iPhone application, you would find argc and argv to be the following (as you can see, here the code is being run in the iPhone Simulator):
Code Listing 5. Finding the values of argc and argv[0] of a simple iPhone application from GDB. The location in argv[0] points to a folder for iPhone Simulator
(gdb) print argc
$1 = 1
Current language: auto; currently objective-c
(gdb) print argv[0]
$2 = 0xbffff14c "/Users/infinitezest/Library/Application Support/iPhone Simulator/User/Applications/B078F029-A061-4801-891C-92DDFDB9E86B/FirstApp.app/FirstApp"
Auto Release Pool and memory management
What you don’t see in regular C programs is the auto release pool (NSAutoreleasePool). This is specific to Cocoa frameworks.
Code Listing 6. The autorelease pool
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[pool release];
The auto release pool functionality is so foundational to the Cocoa frameworks, this class is defined in the Foundation framework (Foundation.framework). Foundation.h is another large header file that contains other header files of basic classes for array, object, thread, etc.
In the above code, when the pool is released, all the objects marked autorelease will get released as well. For example, in the code below, autorelease is used on cell.
Code Listing 7. Autoreleasing an object
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
Main (UIApplicationMain) from the CocoaTouch framework
And finally, the method that hooks into the functionality from the Cocoa framework.
Code Listing 8. UIApplicationMain() method
int retVal = UIApplicationMain(argc, argv, nil, nil);
The method UIApplicationMain() is defined in UIApplication.h header file. This method takes four arguments.
Code Listing 9. Declaration of UIApplicationMain() method in its header file
// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If neither value
// is specified, the UIApplication class is used. The delegate class will be instantiated using init.
UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
The first two arguments are argc and argv - which are passed to the main() method itself. By default, nil has been passed to the last two arguments - principalClassName and delegateClassName. Since no custom principal class is used here, the standard UIApplication class is instantiated. There is one and only one (singleton) instance of UIApplication class. This coordinates and controls the current iPhone application.