xcode Objective-C 属性的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12381108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C properties' types
提问by makaed
When declaring the properties in Objective-C, what are the custom options available to configure, which define how the accessor methods would behave?
在 Objective-C 中声明属性时,可用于配置的自定义选项有哪些,它们定义了访问器方法的行为方式?
For example, you can have weak, strong, readonly.
例如,您可以有弱、强、只读。
回答by PengOne
Here's the short answer:
这是简短的回答:
atomic vs nonatomicprimarily ensures that complete values are returned from synthesized getters and that complete values are written by synthesized setters.
atomic vs nonatomic主要确保完整的值从合成的 getter 返回,完整的值由合成的 setter 写入。
readwrite vs readonlydetermines 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 weak vs retain vs copydetermines how the synthesized accessors interact with the Objective-C memory management scheme.
分配 vs 弱 vs 保留 vs 复制决定了合成访问器如何与 Objective-C 内存管理方案交互。
And now for the long answer:
现在是长答案:
Atomic v Nonatomic
原子与非原子
Assuming that you are @synthesizing the method implementations, atomic vs. non-atomic changes the generated code. If you are writing your own setter/getters, atomic/nonatomic/retain/assign/copy are merely advisory.
假设您正在@synthesizing 方法实现,原子与非原子会更改生成的代码。如果您正在编写自己的 setter/getter,原子/非原子/保留/分配/复制仅仅是建议性的。
With atomic, the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A.
使用 atomic,合成的 setter/getter 将确保始终从 getter 返回或由 setter 设置整个值,而不管任何其他线程上的 setter 活动如何。也就是说,如果线程 A 在 getter 的中间,而线程 B 调用 setter,一个实际可行的值——一个自动释放的对象,最有可能——将返回给 A 中的调用者。
In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than atomic.
在非原子中,没有这样的保证。因此,非原子比原子快得多。
What atomic does not do is make any guarantees about thread safety. If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned -- the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell.
atomic 不做的是对线程安全做任何保证。如果线程 A 在调用 getter 的同时,线程 B 和 C 使用不同的值调用 setter,线程 A 可能会获得返回的三个值中的任何一个——调用任何 setter 之前的值或传递给 setter 的值之一在 B 和 C 中。同样,对象可能以来自 B 或 C 的值结束,无法分辨。
Ensuring data integrity -- one of the primary challenges of multi-threaded programming -- is achieved by other means.
确保数据完整性——多线程编程的主要挑战之一——是通过其他方式实现的。
Assign, weak, retain, copy
分配、弱化、保留、复制
In a nutshell, assign vs weak vs retain vs copy determines how the synthesized accessors interact with the Objective-C memory management scheme:
简而言之,assign、weak、retain、copy 决定了合成访问器如何与 Objective-C 内存管理方案交互:
- assign is the default and simply performs a variable assignment. It does not assert ownership, so the object pointed to by the property's pointer may disappear at any time if no others have asserted ownership themselves through retain or other means. In an ARC environment, assign does not ensure that pointers will not dangle, which means a pointer may end up pointing to junk if the object on the other side has been deallocated.
- weak is identical to assign, except that it will zero out pointers that lead to deallocated objects to stop them from dangling. Weak is only available in an ARC environment.
- retain specifies the new value should be sent -retain on assignment and the old value sent release. Retain is also know as strong.
- copy specifies the new value should be sent -copy on assignment and the old value sent release. Copy is often used for properties in which the type of the property has a mutable cousin (NSArray/NSMutableArray) to prevent others from sending in mutable versions and altering them/having them altered behind their backs, and more.
- assign 是默认值,只是执行变量赋值。它不声明所有权,因此如果没有其他人通过保留或其他方式声明所有权,则该属性指针指向的对象可能随时消失。在 ARC 环境中,assign 不能确保指针不会悬垂,这意味着如果另一侧的对象已被释放,则指针可能最终指向垃圾。
- weak 与assign 相同,除了它将导致释放对象的指针归零以阻止它们悬空。Weak 仅在 ARC 环境中可用。
- 保留指定应发送新值 -retain 分配和旧值发送释放。保留也称为强。
- copy 指定应发送新值 - 赋值时复制,旧值发送释放。复制通常用于属性类型具有可变表亲(NSArray/NSMutableArray)的属性,以防止其他人发送可变版本并更改它们/让它们在背后更改等等。
Remember that retain/strong is done on the created object (it increases the reference count) whereas copy creates a new object. The difference, then, is whether you want to add another retain to the object or create an entirely new object.
请记住,retain/strong 是在创建的对象上完成的(它会增加引用计数),而 copy 会创建一个新对象。区别在于,您是要向对象添加另一个保留还是创建一个全新的对象。