开发者

How to use properties in Objective-C?

开发者 https://www.devze.com 2023-02-14 15:35 出处:网络
When should I use the nonatomic, reta开发者_运维技巧in, readonly and readwrite properties in Objective-C?

When should I use the nonatomic, reta开发者_运维技巧in, readonly and readwrite properties in Objective-C?

For example:

@property(nonatomic, retain) NSObject *myObject;

If I use nonatomic and retain, does this mean that the object will be retained?


First off, I wanted to promote the comment from David Gelhar to a full answer. The modifiers atomic and nonatomic have nothing to do with thread safety. See this question for more detail in that space.

The other items you listed can be addressed relatively simply. I'll hit them briefly and point you toward the documentation on property modifiers if you want more.

atomic vs nonatomic primarily ensures that complete values are returned from synthesized getters and that complete values are written by synthesized setters.

readwrite vs readonly determines whether a synthesized property has a synthesized accessor or not (readwrite has a setter and is the default, readonly does not).

assign vs retain vs copy determines how the synthesized accessors interact with the Objective-C memory management scheme. assign is the default and simply performs a variable assignment. retain specifies the new value should be sent -retain on assignment and the old value sent -release. copy specifies the new value should be sent -copy on assignment and the old value sent -release.


If you use nonatomic, reading and writing the property will not be threadsafe. At this point I don't think it is something you need to worry about, but nonatomic access can be faster than atomic access which is why it is used here.

If you use retain, writing to the property will cause the outgoing value to be released and the incoming value retained, maintaining proper reference-count based ownership of the value.


nontomic Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)

atomic is the default behavior. nonatomic is thread safe. readonly Externally the property will be readonly.

readwrite property will have both the accessor, and the setter.

assign (default) — Specifies that the setter uses simple assignment. retain — Specifies that retain should be invoked on the object upon assignment. This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects)

copy — Specifies that a copy of the object should be used for assignment. The previous value is sent a release message. The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.

0

精彩评论

暂无评论...
验证码 换一张
取 消