IOS, ARC, Property: (readwrite, nonatomic) vs (radwrite, retain, nonatomic)

烂漫一生 提交于 2019-11-28 06:45:04

问题


I am have read up some tutorials on ARC and am still left a bit confused on properties declarations. I wrote most most my code using the following pattern:

@property (readwrite, nonatomic) PlayerData* playerData;
@property (readwrite, nonatomic) MusicLayer* musicLayer;
@property (readwrite, nonatomic) bool isPowerUpAvailable;

Now that I finally started to deal with memory leaks XCode suggested me that in some bits of code I should have added the "retain" keyword in the property declaration.

Using ARC I thought I shouldn't "Bother" about retain counts anymore. Is there some concept I am not getting or missing? Any tutorial references or explanation would be greatly appreciated.


回答1:


ARC is will retain object based on the property declaration, you should use strong for properties that need to be retained and weak for properties that do not need to be retained.

weak properties are also nilled when the object is deallocated.

The compiler will always assume that properties are readwrite so there is no need to declare then this way.

@property (strong, nonatomic) PlayerData* playerData;
@property (strong, nonatomic) MusicLayer* musicLayer;
// Need use assign since strong is for objects only.
@property (assign, nonatomic) bool isPowerUpAvailable;



回答2:


If you prefer continue to use your code, you can exclude ARC only on the specific file .m you want:

Go to Targets > Build Phases > Compile Sources and select your .m file double click on right column of the selection and add -fno-objc-arc so you are exclude ARC only a selected file.

Or if you want to convert all application to new code with ARC, after make a Backup of you project, go to:

Edit > Refactor > Convert to Objective-C ARC and after this do the same but click on Convert to modern Objective-C Sintax

here the screen:

Pay attention not always working before to try duplicate your project!

Hope this help you



来源:https://stackoverflow.com/questions/15702604/ios-arc-property-readwrite-nonatomic-vs-radwrite-retain-nonatomic

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