Objective-C:ARC禁止显式消息发送'retain'

use*_*898 10 objective-c retain automatic-ref-counting

我是Objective-C的新手,我尝试将旧版Objective-C中编写的旧Objective-C项目移植到新版本,但是我收到以下编译器错误:

ARC forbids explicit message send of 'retain'

in 
color = [aColor retain];
or 
color = [[NSColor blackColor] retain];
Run Code Online (Sandbox Code Playgroud)

我正在阅读clang正在使用的新自动引用计数.
我也试过使用Xcode的重构函数,但没有运气......需要替换这个旧代码的正确的Objective-C代码是什么?

tro*_*foe 16

只是:

color = [NSColor blackColor];
Run Code Online (Sandbox Code Playgroud)

ARC将管理对象的生命周期,所以你不需要release,retainautorelease任何更长的时间.


小智 11

ARC的主要优点是编译器将自动清除您在项目中创建的所有对象的引用.所以不需要保留,释放和自动释放.但有些情况下我们想从ARC发布我们的特定文件.在xcode中从ARC发布项目.请按照以下步骤操作.

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" 
Run Code Online (Sandbox Code Playgroud)

此标志将在xcode中从您的编译器的ARC中释放该特定文件.

我希望这可以帮助您完成所有项目.