ios - 'autorelease is unavailable' errors and 'ARC forbids explicit message send of autorelease' errors

StackOverflow https://stackoverflow.com/questions/17412074

문제

I had an app that was working just fine. And then I tried to embed the navigation controller into a tabbarcontroller and next thing I know I started getting these errors during compiling.

Would anyone know why these happen? Did some setting get unchecked or checked by accident?

Thanks, Alex

도움이 되었습니까?

해결책

Seems your previously working code did not use ARC, now you tried to embed it into code which uses ARC ... Refactor your code using "Edit->Convert->Convert to Object-C ARC"

다른 팁

ARC is enabled per translation -- every compiled source file and everything it sees via inclusion must abide by the ARC or MRC. And yes, the modes can coexist (i.e. you can have ARC on for some files, but not all and the libraries you link to can use either).

You have two modes:

ARC

The expression [obj autorelease] is forbidden. ARC will add it for you (unless you have unusual reference counting sequences).

Under typical scenarios, you can just write:

// a method which returns an autoreleased object
- (NSArray *)something
{
  return [[NSArray alloc] initWithObjects:…YOUR_OBJECTS…];
}

and then ARC will add the autorelease for you.

But if you write:

- (NSArray *)something
{
  return [[[NSArray alloc] initWithObjects:…YOUR_OBJECTS…] autorelease];
}

in ARC, it will be a compile error (like the one in your title).

MRC

And this is the MRC form:

- (NSArray *)something
{
  return [[[NSArray alloc] initWithObjects:…YOUR_OBJECTS…] autorelease];
}

Your project probably uses ARC by default (i.e. it is defined in an xcconfig, at the project level, or at the target level), though you have added a source file which was written for MRC.

Since the file is either compiled as ARC, you can either remove the autorelease message or disable ARC for the single file.

The errors are on the new code?

In that case I think your projects is ARC-enabled and when you tried to embed the UINavigationController you inserted some non-ARC code.

Did you change the compiler?

LLVM compiler introduces ARC. If you were developing a non-ARC project, maybe you just compiled with LLVM and that broke your code.

Try refactoring the code. Check this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top