When should I use the nonatomic
, retain
, readonly
and readwrite
properties in Objective-C?
我什么时候应该在Objective-C中使用非原子,保留,只读和读写属性?
For example:
@property(nonatomic, retain) NSObject *myObject;
If I use nonatomic
and retain
, does this mean that the object will be retained?
如果我使用非原子并保留,这是否意味着该对象将被保留?
3 个解决方案
#1
10
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.
首先,我想将David Gelhar的评论推广到一个完整的答案。原子和非原子的修饰符与线程安全无关。有关该空间的更多详细信息,请参阅此问题。
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.
原子与非原子主要确保从合成的getter返回完整的值,并且完整的值由合成的setter写入。
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).
readwrite vs readonly确定合成属性是否具有合成访问器(readwrite具有setter并且是默认值,readonly不具有)。
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
.
assign vs retain vs copy确定合成访问器如何与Objective-C内存管理方案交互。 assign是默认值,只是执行变量赋值。 retain指定应该在发送时发送新值-retain并且发送旧值-release。 copy指定新值应该在赋值时发送-copy并且旧值发送-release。
#2
2
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.
如果使用retain,则写入属性将导致释放传出值并保留传入值,从而维护基于引用计数的正确值。
#3
1
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.)
nontomic基本上,如果你说非原子,并使用@synthesize生成访问器,那么如果多个线程尝试一次更改/读取属性,则可能发生错误。您可以获得部分写入的值或过度释放/保留的对象,这很容易导致崩溃。 (但这可能比原子访问器快得多。)
atomic is the default behavior. nonatomic is thread safe. readonly Externally the property will be readonly.
atomic是默认行为。非原子是线程安全的。 readonly外部财产将是只读的。
readwrite property will have both the accessor, and the setter.
readwrite属性将同时具有访问器和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)
assign(默认值) - 指定setter使用简单赋值。 retain - 指定在分配时应在对象上调用retain。此属性仅对Objective-C对象类型有效。 (您无法为Core Foundation对象指定retain)
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.
copy - 指定应使用对象的副本进行分配。先前的值将发送一条释放消息。通过调用复制方法进行复制。此属性仅对对象类型有效,对象类型必须实现NSCopying协议。
#1
10
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.
首先,我想将David Gelhar的评论推广到一个完整的答案。原子和非原子的修饰符与线程安全无关。有关该空间的更多详细信息,请参阅此问题。
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.
原子与非原子主要确保从合成的getter返回完整的值,并且完整的值由合成的setter写入。
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).
readwrite vs readonly确定合成属性是否具有合成访问器(readwrite具有setter并且是默认值,readonly不具有)。
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
.
assign vs retain vs copy确定合成访问器如何与Objective-C内存管理方案交互。 assign是默认值,只是执行变量赋值。 retain指定应该在发送时发送新值-retain并且发送旧值-release。 copy指定新值应该在赋值时发送-copy并且旧值发送-release。
#2
2
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.
如果使用retain,则写入属性将导致释放传出值并保留传入值,从而维护基于引用计数的正确值。
#3
1
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.)
nontomic基本上,如果你说非原子,并使用@synthesize生成访问器,那么如果多个线程尝试一次更改/读取属性,则可能发生错误。您可以获得部分写入的值或过度释放/保留的对象,这很容易导致崩溃。 (但这可能比原子访问器快得多。)
atomic is the default behavior. nonatomic is thread safe. readonly Externally the property will be readonly.
atomic是默认行为。非原子是线程安全的。 readonly外部财产将是只读的。
readwrite property will have both the accessor, and the setter.
readwrite属性将同时具有访问器和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)
assign(默认值) - 指定setter使用简单赋值。 retain - 指定在分配时应在对象上调用retain。此属性仅对Objective-C对象类型有效。 (您无法为Core Foundation对象指定retain)
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.
copy - 指定应使用对象的副本进行分配。先前的值将发送一条释放消息。通过调用复制方法进行复制。此属性仅对对象类型有效,对象类型必须实现NSCopying协议。