Objective-C声明@property属性(nonatomic, copy, strong, weak)

时间:2022-09-07 10:19:54

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? Some sort of example would be great also. I am using ARC.

当我必须使用每个属性时,有人会详细地向我解释:非原子的、复制的、强的、弱的,等等,对于一个已声明的属性,并解释每个属性的作用?有些例子也很好。我用弧。

4 个解决方案

#1


548  

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. 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.

nonatomic用于多线程目的。如果我们在声明的时候设置了nonatomic属性,那么任何其他想要访问该对象的线程都可以访问它并给出关于多线程的结果。

Copy

复制

copy is required when the object is mutable. 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. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)

赋值多少有点与复制相反。当调用一个赋值属性的getter时,它返回一个对实际数据的引用。当您拥有原始类型的属性时,通常使用此属性(float、int、BOOL…)

Retain

保留

retain is required when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count to) the object. 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.

当属性是指向对象的指针时,需要保留。@synthesize生成的setter将保留(也就是添加一个retain count to)对象。当您完成该对象时,您将需要释放该对象。通过使用retain,它将增加autorelease池中的retain count和occupy内存。

Strong

强大的

strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.

strong是retain属性的替代,作为Objective-C自动引用计数(ARC)的一部分。在非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

这是一个很好的网站,可以了解到ios5的强大和薄弱。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. 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.

弱类似于强,只是它不会增加引用计数1。它并没有成为那个对象的所有者,但只是引用它。如果对象的引用计数下降到0,即使您仍然指向它,它将从内存中释放。

The above link contain both Good information regarding Weak and Strong.

上面的链接包含了关于弱和强的信息。

#2


45  

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.

strong与ARC一起使用,它基本上可以帮助你,不用担心一个对象的保留计数。当您完成它时,ARC会自动为您释放它。使用强大的关键字表示您拥有该对象。

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.

弱所有权意味着你不拥有它,它只会跟踪对象直到它被分配到的对象,一旦第二个对象被释放,它就失去了价值。如。obj.a = objectB;使用和a的属性较弱,其值只会在objectB仍在内存中有效。

copy property is very well explained here

复制属性在这里解释得很好。

strong,weak,retain,copy,assign are mutually exclusive so you can't use them on one single object... read the "Declared Properties " section

强、弱、保留、复制、分配是互斥的,所以不能在一个对象上使用它们……阅读“声明属性”部分。

hoping this helps you out a bit...

希望这能帮你……

#3


17  

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.

分配意味着__unsafe_unretained所有权。

copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter.

复制意味着拥有强大的所有权,以及在setter中复制语义的通常行为。

retain implies __strong ownership.

保留所有权意味着强烈。

strong implies __strong ownership.

强烈暗示强烈的所有权。

unsafe_unretained implies __unsafe_unretained ownership.

unsafe_unretained意味着__unsafe_unretained所有权。

weak implies __weak ownership.

弱意味着__weak所有权。

#4


9  

Great answers! One thing that I would like to clarify deeper is nonatomic/atomic. The user should understand that this property - "atomicity" spreads only on the attribute's reference and not on it's contents. I.e. atomic will guarantee the user atomicity for reading/setting the pointer and only the pointer to the attribute. 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. BUT the dict itself (the dictionary dict pointing to) is still thread unsafe, i.e. all read/add operations to the dictionary are still thread unsafe.

在这种情况下,可以保证命令的指针将被不同的线程以原子方式读取/设置。但是字典本身(字典上的命令指向)仍然是线程不安全的,即所有对字典的读/添加操作仍然是线程不安全的。

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".

如果您需要线程安全的集合,您要么有糟糕的体系结构(更常见),要么有实际需求(更少)。如果它是“真正的需求”——你应该找到好的、经过测试的线程安全收集组件,或者准备好进行测试和编写自己的测试。后一种情况是“无锁”、“无等待”的范例。乍一看就像火箭科学,但它可以帮助你在“通常锁定”的情况下取得出色的表现。

#1


548  

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. 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.

nonatomic用于多线程目的。如果我们在声明的时候设置了nonatomic属性,那么任何其他想要访问该对象的线程都可以访问它并给出关于多线程的结果。

Copy

复制

copy is required when the object is mutable. 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. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)

赋值多少有点与复制相反。当调用一个赋值属性的getter时,它返回一个对实际数据的引用。当您拥有原始类型的属性时,通常使用此属性(float、int、BOOL…)

Retain

保留

retain is required when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count to) the object. 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.

当属性是指向对象的指针时,需要保留。@synthesize生成的setter将保留(也就是添加一个retain count to)对象。当您完成该对象时,您将需要释放该对象。通过使用retain,它将增加autorelease池中的retain count和occupy内存。

Strong

强大的

strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.

strong是retain属性的替代,作为Objective-C自动引用计数(ARC)的一部分。在非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

这是一个很好的网站,可以了解到ios5的强大和薄弱。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. 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.

弱类似于强,只是它不会增加引用计数1。它并没有成为那个对象的所有者,但只是引用它。如果对象的引用计数下降到0,即使您仍然指向它,它将从内存中释放。

The above link contain both Good information regarding Weak and Strong.

上面的链接包含了关于弱和强的信息。

#2


45  

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.

strong与ARC一起使用,它基本上可以帮助你,不用担心一个对象的保留计数。当您完成它时,ARC会自动为您释放它。使用强大的关键字表示您拥有该对象。

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.

弱所有权意味着你不拥有它,它只会跟踪对象直到它被分配到的对象,一旦第二个对象被释放,它就失去了价值。如。obj.a = objectB;使用和a的属性较弱,其值只会在objectB仍在内存中有效。

copy property is very well explained here

复制属性在这里解释得很好。

strong,weak,retain,copy,assign are mutually exclusive so you can't use them on one single object... read the "Declared Properties " section

强、弱、保留、复制、分配是互斥的,所以不能在一个对象上使用它们……阅读“声明属性”部分。

hoping this helps you out a bit...

希望这能帮你……

#3


17  

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.

分配意味着__unsafe_unretained所有权。

copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter.

复制意味着拥有强大的所有权,以及在setter中复制语义的通常行为。

retain implies __strong ownership.

保留所有权意味着强烈。

strong implies __strong ownership.

强烈暗示强烈的所有权。

unsafe_unretained implies __unsafe_unretained ownership.

unsafe_unretained意味着__unsafe_unretained所有权。

weak implies __weak ownership.

弱意味着__weak所有权。

#4


9  

Great answers! One thing that I would like to clarify deeper is nonatomic/atomic. The user should understand that this property - "atomicity" spreads only on the attribute's reference and not on it's contents. I.e. atomic will guarantee the user atomicity for reading/setting the pointer and only the pointer to the attribute. 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. BUT the dict itself (the dictionary dict pointing to) is still thread unsafe, i.e. all read/add operations to the dictionary are still thread unsafe.

在这种情况下,可以保证命令的指针将被不同的线程以原子方式读取/设置。但是字典本身(字典上的命令指向)仍然是线程不安全的,即所有对字典的读/添加操作仍然是线程不安全的。

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".

如果您需要线程安全的集合,您要么有糟糕的体系结构(更常见),要么有实际需求(更少)。如果它是“真正的需求”——你应该找到好的、经过测试的线程安全收集组件,或者准备好进行测试和编写自己的测试。后一种情况是“无锁”、“无等待”的范例。乍一看就像火箭科学,但它可以帮助你在“通常锁定”的情况下取得出色的表现。