{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Understanding a Typical main Method in Objective-C for iPhone Development
Summary
For iPhone and iPod Touch applications, you would consistently see the same main() method, where an application starts. This article explains various pieces of that main() method.
 
Table of Contents

The Starting point

Code Listing 1. The main() method in Objective-C programs

Figure 1. The annotated main method.

The import

Code Listing 2. Importing UIKit

UIKit Framework

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

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.

Need for UIKit.h in main.m

The main() signature

Code Listing 4. Signature of the main() method

The arguments

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

Auto Release Pool and memory management

Code Listing 6. The autorelease pool

Code Listing 7. Autoreleasing an object

Main (UIApplicationMain) from the CocoaTouch framework

Code Listing 8. UIApplicationMain() method

Code Listing 9. Declaration of UIApplicationMain() method in its header file

 
Article Series
Previous Article:
Test Your Knowledge: Source Code of First iPhone Application Explained
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: Understanding a Typical main Method in Objective-C for iPhone Development

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.

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 <UIKit/UIKit.h>

#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.

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.

Take a Quick Quiz on this Article

1. What is the starting point of execution for an iPhone application?



: This is an Objective-C application
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: Source Code of First iPhone Application Explained
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: Understanding a Typical main Method in Objective-C for iPhone Development
Bookmark and Share This

More Articles With Similar Tags
This is a test on the article that gives a quick overview of the development tools installed with iPhone SDK. These tools include Xcode, Interface Builder, Instruments, and iPhone Simulator. In the article, Dash Code, which is used to build web applications for iPhone is also introduced. GDB, the open source debugger, is discussed there as well.
icon-xcode-groups-files.jpg
This article discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
This is a test on the article that discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
icon-xcode-interface-builder-connection-panel.jpg
This article explains the user interface files of an iPhone application. Explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
This is a test on the article that explains the user interface files of an iPhone application. It explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
About  Contact  Privacy Policy  Site Map