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

11

我是Objective-C的新手,我试图将一个旧版本的Objective-C项目移植到新版本,但是我遇到了以下编译器错误:

ARC forbids explicit message send of 'retain'

in 
color = [aColor retain];
or 
color = [[NSColor blackColor] retain];

我在阅读关于clang现在使用的新的自动引用计数的内容。
我还尝试使用Xcode的重构功能,但没有成功...
请问应该用什么样的Objective-C代码来替换这个旧代码?

3个回答

17

简单地说:

color = [NSColor blackColor];

ARC将管理您的对象生命周期,因此您不再需要使用releaseretainautorelease了。


12

ARC的主要优点是编译器将自动清除您在项目中创建的所有对象的引用。因此,不需要使用retain、release和autorelease。但是,在某些情况下,我们希望从ARC中释放特定的文件。请按照以下步骤释放xcode中的项目。

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" 

这个标志将在Xcode中从编译器的ARC中释放那个特定的文件。

我希望这对你的所有项目都有帮助。


3

只需移除retain,如下:

color = [NSColor blackColor]

使用 ARC,您不能使用 retainreleaseautorelease,因为它们会自动进行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接