简体   繁体   中英

iOS5 @property changes

In iOS 5, "retain" and "release" are not supported any more. Instead "strong" and "weak" are the new way.

iOS 4 code:

@property(nonatomic, retain)
@property(nonatomic, assign)

iOS 5 code:

???
???

"In iOS 5, retain release are not supported any more." They are, just not when using ARC.

When using ARC, -[<NSObject> retain] is a no-op.

For properties, you can use strong if using ARC but that's not required (you can use retain too if you like). strong and retain are identical:

@property(nonatomic, strong)
@property(nonatomic, assign)

Just make sure you are consistent (don't use both strong and retain in the same project).

它们不完全相同,但基本上retain := strongassign := weak我建议看一下ARC发行说明

nonatomic property states that the object is not thread safe which means if a different thread tries to access this object than bad things can happen but this is much faster than atomic property.

strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.

weak ownership means that you don't own it and it just keeps track of the object till the object it was assigned to stays , as soon as the second object is released it loses is value. For eg. obj.a=objectB; is used and a has weak property , than its value will only be valid till objectB remains in memory.

copy property is very well explained here https://stackoverflow.com/a/5002646/919545

strong,weak,retain,copy,assign are mutually exclusive so you can't use them on one single object... read the "Declared Properties " section of http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

hoping this helps you out a bit...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM