I'm trying to get a better understanding of the purpose of each file contained in a basic iOS application.
Is there any reason to modify the main.m file? I'm wondering if that file ever needs to be touched. If you do modify it, why?
An M file is a class implementation file used by programs written in Objective-C. It begins with the @implementation directive and initializes variables and functions that can be referenced by other Objective-C source files. M files may also reference header (. H) files.
The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.
The file name “AppDelegate. m” is a generic reference to “the file that contains the definition of your application delegate class”. Your app is not required to name the file “AppDelegate.
h is called the Header file and the . m is called the Implementation file. Together, they form the AppDelegate class. Wikipedia describes a class as: In object-oriented programming, a class is a construct that is used to define a distinct type.
In 99.9% of all cases, there is no need to touch main.m
.
In the other 0.1%, you might want to change the arguments of the call to the UIApplicationMain()
function. The last two arguments of this function specify the names of the classes that represent your main application (UIApplication
by default) and the application delegate.
Should you ever decide to subclass UIApplication
, you would set the third argument to the name of your subclass. Subclassing UIApplication
can be useful if you want to intercept certain events your app handles (override sendEvent:
).
The name of your app delegate class might change if you simply decide to rename that class. Also, if the fourth argument to UIApplicationMain()
is nil
(which is the default in project templates that do not use Storyboarding in iOS 5), it signifies that you create your app delegate object in your app's main NIB file. Should you ever decide to change that decision (e.g., to adapt Storyboarding for an existing project), you would have to change the fourth argument in order to tell UIApplicationMain()
the name of the class it should instantiate.
There are certain cases where you might want to modify this file. By default, the iOS development templates assume that you'll be using Interface Builder to provide your initial interface, and do so with the presence of a nil value as the last argument to UIApplicationMain()
, like in the following:
return UIApplicationMain(argc, argv, nil, nil);
If you wish to contruct your interface programmatically, you may need to specify your application delegate class using that last parameter:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([SPAppDelegate class]));
This is so that the application knows where to start in constructing your interface. With one constructed using Interface Builder, you indicate which IB file to use as the base through one of your Info.plist keys.
If you have a main.m file created prior to automatic reference counting, you might have an explicit NSAutoreleasePool that wraps this function:
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int retval = UIApplicationMain(argc, argv, nil, NSStringFromClass([SPAppDelegate class]));
[pool release];
return retval;
Under ARC, this would be converted to an @autoreleasepool
:
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil,NSStringFromClass([SPAppDelegate class]));
return retVal;
}
These are the only two cases where I've edited the main.m file in any way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With