0

I'm a little confused because I have a class that uses the this 1st main.m in its example code while my project uses the latter. I am having a whole lot of trouble getting the class to instance to with my applicationDidFinishLaunching and I think that these different main.m files might be the culprit. The .xib files between my project and the example project are identical as are the .plist settings for Main Nib name. The delegates are all linked up correctly

#import <UIKit/UIKit.h>
#import "MidiTestingAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MidiTestingAppDelegate class]));
    }
}

And

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
#if __has_feature(objc_arc)
    @autoreleasepool
    {
        int retVal = UIApplicationMain(argc, argv, nil, nil);
        return retVal;
    }
#else
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
#endif
}

I assume that the former goes right to the App Delegate while the latter loads the UIApplication directly? Is that correct?

frankie
  • 661
  • 2
  • 10
  • 25
  • 1
    The second one has two compile-time branches: which one will actually be compiled depends on if ARC is enabled or not. – Rok Jarc Jun 08 '12 at 13:23
  • I hope checks like that are not scattered throughout the whole project... – Joe Jun 08 '12 at 13:26
  • the Arc checks are scattered throughout the entire class I downloaded. Is that going to be problematic? – frankie Jun 08 '12 at 13:45
  • Right, I see that there are two branches - one for Arc and one without. I am wondering how they differ in what they load as one uses the delegate while the other does not. My confusion is because the delegate is set up exactly the same in both project NIBs. But only the example code (the one that checks for Arc) loads. I guess my question is, what are these two main.m doing that is different, besides checking for ARC. – frankie Jun 08 '12 at 13:50
  • 1
    The thing is, you can still use the new @autorelease pool syntax even in non-ARC projects, as long as you have a recent Xcode version. The compiler figures it out. – Mike Weller Jun 08 '12 at 13:57

2 Answers2

4

There's a lot of bad information here.

First of all, assuming you have a recent version of Xcode, the first version is all you need.

The @autoreleasepool syntax will work in non-ARC projects. The compiler figures it all out and will generate the appropriate code in either case.

The second piece of code uses a feature detection macro (__has_feature) to decide between the new @autoreleasepool syntax and the legacy NSAutoreleasePool method of creating a pool, depending on whether ARC is enabled. You would only need to use the second piece of code if you are running an old Version of Xcode which doesn't have the new syntax available.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
0

The second example is bad form. This is because @autoreleasepool can be used in non-ARC projects, and is actually faster (link) than creating your own pool.

Community
  • 1
  • 1
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82