Objective-C: ARC forbids explicit message send of 'retain'

↘锁芯ラ 提交于 2020-01-20 05:47:27

问题


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?


回答1:


Simply:

color = [NSColor blackColor];

ARC will manage the lifetime of your objects so you don't need release, retain or autorelease any longer.




回答2:


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.




回答3:


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

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