NSAutoreleasePool is unavailable

前端 未结 5 1976
余生分开走
余生分开走 2020-12-05 23:13

I am following \"Programming in Objective-C\" 3rd edition and I am having problems with the first example.

I keep getting this error:

相关标签:
5条回答
  • 2020-12-05 23:45

    In my case, I wanted ARC on, and wanted to update a sample project to work properly. Apple's NSAutoReleasePool docs are technically correct, but don't come straight out and explain this. Here's how:

    Take your application main, which probably looks something like this:

    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([DemoAppDelegate class]));
    
        [pool release];
    
        return retVal;
    }
    

    And change it to look like this:

    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([DemoAppDelegate class]));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 23:47

    The compiler is being asked to compile the file with ARC (automatic reference counting) enabled. Turn that off or, better yet, modernize your example:

    int main (int argc, const char * argv[]) {
        @autoreleasepool {
            NSLog (@"Programming is fun!");
        }
        return 0;
    }
    

    (No, I can't tell you how, specifically, to turn off ARC, if that was the route you were to go down due to the aforementioned NDA.)

    0 讨论(0)
  • 2020-12-05 23:51

    Quick post just in case you still looking

    You can disable ARC in build settings.

    • Click on you project, in the left hand organizer.
    • Select your target, in the next column over.
    • Select the Build Settings tab at the top.
    • Scroll down to "Objective-C Automatic Reference Counting" (it may be listed as "CLANG_ENABLE_OBJC_ARC" under the User-Defined settings group), (if you do not find ARC option under build settings, you might need to toggle you compiler. You can find it under build settings)
    • and set it to NO.
    0 讨论(0)
  • 2020-12-05 23:53

    Here is a link to Apple's transition guide to ARC.

    OK...check this out. Specific change to NSAutoreleasePool - this is how Xcode initializes itself when you create your first app. I don't know about you, but I love this idea!

    No worries if you are following along w/ Kochan's book. When starting your project, just uncheck the "Use ARC" box. Everything will work.

    0 讨论(0)
  • 2020-12-06 00:03

    ARC is enabled when you first create a new project. Right know the only way I know how to enable or not enable it is when you first create your program. It is one of the checkboxes you have to unselect.

    0 讨论(0)
提交回复
热议问题