本文翻译自:Objective-C declared @property attributes (nonatomic, copy, strong, weak)
Can someone explain to me in detail when I must use each attribute: nonatomic
, copy
, strong
, weak
, and so on, for a declared property, and explain what each does? 当我必须使用每个属性( nonatomic
, nonatomic
copy
, strong
, weak
等等)来声明属性时,有人可以向我详细解释吗? Some sort of example would be great also. 某种例子也很好。 I am using ARC. 我正在使用ARC。
#1楼
参考:https://stackoom.com/question/FmXJ/Objective-C声明的-property属性-非原子-复制-强-弱
#2楼
Great answers! 好答案! One thing that I would like to clarify deeper is nonatomic
/ atomic
. 我想更深入地阐明的一件事是非nonatomic
/ atomic
。 The user should understand that this property - "atomicity" spreads only on the attribute's reference and not on it's contents. 用户应了解,此属性-“原子性”仅在属性的引用上传播,而不在属性的内容上传播。 Ie atomic
will guarantee the user atomicity for reading/setting the pointer and only the pointer to the attribute. 也就是说, atomic
将保证用户用于读取/设置指针以及仅指向属性的指针的原子性。 For example: 例如:
@interface MyClass: NSObject
@property (atomic, strong) NSDictionary *dict;
...
In this case it is guaranteed that the pointer to the dict
will be read/set in the atomic manner by different threads. 在这种情况下,可以确保由不同线程以原子方式读取/设置指向dict
的指针。 BUT the dict
itself (the dictionary dict
pointing to) is still thread unsafe , ie all read/add operations to the dictionary are still thread unsafe. 但是dict
本身(指向字典的dict
)仍然是线程不安全的 ,即对字典的所有读取/添加操作仍然是线程不安全的。
If you need thread safe collection you either have bad architecture (more often) OR real requirement (more rare). 如果您需要线程安全收集,那么要么架构不好(更多),要么实际需求(更罕见)。 If it is "real requirement" - you should either find good&tested thread safe collection component OR be prepared for trials and tribulations writing your own one. 如果这是“真正的需求”,则您应该找到经过良好测试的线程安全集合组件,或者准备编写自己的组件进行试验和磨难。 It latter case look at "lock-free", "wait-free" paradigms. 后一种情况看“无锁”,“无等待”范式。 Looks like rocket-science at a first glance, but could help you achieving fantastic performance in comparison to "usual locking". 乍一看就像火箭科学,但与“通常的锁定”相比,可以帮助您实现出色的性能。
#3楼
This link has the break down 该链接有故障
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#ownership.spelling.property http://clang.llvm.org/docs/AutomaticReferenceCounting.html#ownership.spelling.property
assign implies __unsafe_unretained ownership. Assign表示__unsafe_unretain所有权。
copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter. 复制暗含__strong所有权,以及在setter上复制语义的通常行为。
retain implies __strong ownership. 保留表示__具有强烈所有权。
strong implies __strong ownership. 强意味着__strong所有权。
unsafe_unretained implies __unsafe_unretained ownership. unsafe_unretained隐含__unsafe_unretained所有权。
weak implies __weak ownership. 弱表示__弱所有权。
#4楼
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. nonatomic
属性指出该对象不是线程安全的,这意味着如果其他线程尝试访问该对象可能发生不良情况,但这比原子属性快得多。
strong
is used with ARC and it basically helps you , by not having to worry about the retain count of an object. strong
与ARC一起使用,它基本上可以帮助您,而不必担心对象的保留计数。 ARC automatically releases it for you when you are done with it.Using the keyword strong
means that you own the object. 当你与it.Using关键字做ARC自动释放它为你strong
,你自己的目标的手段。
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. weak
所有权意味着您不拥有它,它只会跟踪对象,直到分配给它的对象留下为止,一旦释放第二个对象,它就会失去价值。 For eg. 例如。 obj.a=objectB;
is used and a has weak property , than its value will only be valid till objectB remains in memory. 使用且具有弱属性,则其值仅在objectB保留在内存中之前才有效。
copy
property is very well explained here copy
属性在这里很好解释
strong,weak,retain,copy,assign
are mutually exclusive so you can't use them on one single object... read the "Declared Properties " section strong,weak,retain,copy,assign
是互斥的,因此您不能在单个对象上使用它们。请阅读“声明的属性”部分
hoping this helps you out a bit... 希望这对您有所帮助...
#5楼
This answer has numerous errors and is also outdated. 这个答案有很多错误,而且已经过时了。 Please see other questions/answers and the comments. 请查看其他问题/答案和评论。
Nonatomic 非原子
nonatomic
is used for multi threading purposes. nonatomic
用于多线程目的。 If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading. 如果我们在声明时设置了非原子属性,那么任何其他想要访问该对象的线程都可以访问它,并给出有关多线程的结果。
Copy 复制
copy
is required when the object is mutable. 当对象可变时,需要copy
。 Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. 如果您现在需要该对象的值,并且不希望该值反映该对象的其他所有者所做的任何更改,请使用此值。 You will need to release the object when you are finished with it because you are retaining the copy. 完成对象后,您将需要释放它,因为您保留了副本。
Assign 分配
Assign
is somewhat the opposite to copy
. Assign
与copy
相反。 When calling the getter of an assign
property, it returns a reference to the actual data. 调用assign
属性的getter时,它将返回对实际数据的引用。 Typically you use this attribute when you have a property of primitive type (float, int, BOOL...) 通常,当您具有基本类型的属性(float,int,BOOL ...)时,可以使用此属性
Retain 保留
retain
is required when the attribute is a pointer to an object. 当属性是指向对象的指针时, retain
是必需的。 The setter generated by @synthesize
will retain (aka add a retain count to) the object. @synthesize
生成的设置器将保留(也向其添加保留计数)对象。 You will need to release the object when you are finished with it. 完成处理后,您将需要释放该对象。 By using retain it will increase the retain count and occupy memory in autorelease pool. 通过使用保留,它将增加保留计数并占用自动释放池中的内存。
Strong 强大
strong
is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). strong
是对保留属性的替代,这是Objective-C自动参考计数(ARC)的一部分。 In non-ARC code it's just a synonym for retain. 在非ARC代码中,它只是保留的同义词。
This is a good website to learn about strong
and weak
for iOS 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1 这是一个很好的网站,可以了解iOS 5的strong
和weak
。http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
Weak 弱
weak
is similar to strong
except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. weak
与“ strong
类似,不同之处在于它不会将引用计数增加1。它不会成为该对象的所有者,而仅持有对该对象的引用。 If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory. 如果对象的引用计数降至0,即使您在此处仍指向它,它也会从内存中释放。
The above link contain both Good information regarding Weak and Strong. 上面的链接包含有关弱和强的良好信息。