I'm new to Objective-C, I try to port an old Objective-C project written in an older version of Objective-C to the new one, but I'm getting the following compiler error:
ARC forbids explicit message send of 'retain'
in
color = [aColor retain];
or
color = [[NSColor blackColor] retain];
I was reading about the new automatic reference counting that clang is using now.
I have also tried to use Xcode's refactor function but with no luck...
What is the proper Objective-C code that need to replace this old code?
Simply:
color = [NSColor blackColor];
ARC will manage the lifetime of your objects so you don't need release
, retain
or autorelease
any longer.
The main advantage of ARC is the compiler will clear the references of all objects automatically that you have created in your projects. So There is no need of retain, release, & autorelease. But some cases we want to release our particular files from ARC. To release your project from ARC in xcode. Please follow the following steps.
1.Click your project for Build Phases.
2.Click the drop down menu named as "Compile Sources".
3.Double Click the file that you want to free from ARC.
4.Type the following to set the compiler flag.
"-fno-objc-arc"
This flag will release that particular file from ARC of your Compiler in xcode.
I hope this will help you for all of your projects.
Just remove retain, so:
color = [NSColor blackColor]
With ARC, you cannot use retain
, release
, autorelease
as it does them for you.
来源:https://stackoverflow.com/questions/14463690/objective-c-arc-forbids-explicit-message-send-of-retain