Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

objective c - strong / weak / retain / unsafe_unretained / assign

properties for synthesizing the property : retain / assign

  • retain - it is retained, old value is released and it is assigned
  • assign - it is only assigned

properties for ownership : IOS5 = strong / weak IOS4 = retain / unsafe_unretained

  • strong (iOS4 = retain) - i'am the owner, you cannot dealloc this before aim fine with that = retain

  • weak (iOS4 = unsafe_unretained) - the same thing as assign, no retain or release

so unsafe_unretained == assign?

@property (nonatomic, assign) NSArray * tmp;

is equal to ?

@property (nonatomic, unsafe_unretained) NSArray * tmp;

and vice versa ?

if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign?

and a delegate in iOS4 should be unsafe_unretained or assign?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign?

you should use unsafe_unretained. You want to show the reader of your code that you actually wanted to use weak but that this was not possible because weak is not available on the iOS version you want to deploy.

One day you will drop the support for iOS4. And then you could just search for unsafe_unretained and replace all of them with weak. This will be much easier than searching for assign and figuring out if you actually meant assign or weak

The use of unsafe_unretained creates more readable and understandable code where the intentions of the developer are easier to see. Basically the same reason we use YES instead of 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...