Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...