NSAutoreleasePool is unavailable

别来无恙 提交于 2019-12-17 16:25:06

问题


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

I keep getting this error:

Semantic Issue: 'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode

Here is my code:

//
// main.m
// prog1 //
// Created by Steve Kochan on 1/30/11.
// Copyright 2011 ClassroomM, Inc.. All rights reserved. //

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog (@"Programming is fun!");
    [pool drain];
    return 0;
}

Any insight will be greatly appreciated.


回答1:


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




回答2:


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.



回答3:


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]));
    }
}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/6655842/nsautoreleasepool-is-unavailable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!