在Objective-C中保持,分配,复制,非原子

时间:2022-09-07 10:15:16

As someone that's new to Objective-C can someone give me an overview of the retain, assign, copy and any others I'm missing, that follow the @property directive? What are they doing and why would I want to use one over another?

作为Objective-C的新手,有人能给我一个关于retain, assign, copy和其他我所缺少的,遵循@property指令的概述吗?他们在做什么,为什么我要用一个来代替另一个?

4 个解决方案

#1


263  

The article linked to by MrMage is no longer working. So, here is what I've learned in my (very) short time coding in Objective-C:

由MrMage链接的文章不再有效。这是我在Objective-C中(非常)短时间编码学到的东西:

nonatomic vs. atomic - "atomic" is the default. Always use "nonatomic". I don't know why, but the book I read said there is "rarely a reason" to use "atomic". (BTW: The book I read is the BNR "iOS Programming" book.)

非原子vs原子-“原子”是默认值。总是使用“原子”。我不知道为什么,但我读过的那本书说“很少有理由”使用“原子”。(顺便说一句:我读的书是BNR的《iOS编程》。)

readwrite vs. readonly - "readwrite" is the default. When you @synthesize, both a getter and a setter will be created for you. If you use "readonly", no setter will be created. Use it for a value you don't want to ever change after the instantiation of the object.

readwrite vs. readonly -“readwrite”是默认值。当您@synthesize时,会为您创建一个getter和setter。如果使用“readonly”,则不会创建任何setter。将它用于对象实例化后不希望更改的值。

retain vs. copy vs. assign

保留、复制、分配

  • "assign" is the default. In the setter that is created by @synthesize, the value will simply be assigned to the attribute. My understanding is that "assign" should be used for non-pointer attributes.
  • “分配”是默认的。在@synthesize创建的setter中,只需将值分配给属性。我的理解是“assign”应该用于非指针属性。
  • "retain" is needed when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count) the object. You will need to release the object when you are finished with it.
  • 当属性是指向对象的指针时,需要“retain”。@synthesize生成的setter将保留(也称为添加保留计数)对象。当您完成该对象时,您将需要释放它。
  • "copy" is needed 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.
  • 当对象是可变的时,需要“复制”。如果您需要此时对象的值,并且不希望该值反映对象的其他所有者所做的任何更改,那么请使用该值。当您完成该对象时,您将需要释放该对象,因为您将保留该副本。

#2


270  

Before you know about the attributes of @property, you should know what is the use of @property.

在您了解@property的属性之前,您应该了解@property的用途。

  • @property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.

    @property提供了一种定义类要封装的信息的方法。如果您使用@property声明一个对象/变量,那么该对象/变量将被导入其类的其他类访问。

  • If you declare an object using @property in the header file, then you have to synthesize it using @synthesize in the implementation file. This makes the object KVC compliant. By default, compiler will synthesize accessor methods for this object.

    如果在头文件中使用@property声明一个对象,那么必须在实现文件中使用@synthesize合成它。这使得对象KVC兼容。默认情况下,编译器将为该对象合成访问器方法。

  • accessor methods are : setter and getter.

    访问器方法是:setter和getter。

Example: .h

例子:. h

@interface XYZClass : NSObject
@property (nonatomic, retain) NSString *name;
@end

.m

00

@implementation XYZClass
@synthesize name;
@end

Now the compiler will synthesize accessor methods for name.

现在编译器将为名称合成访问器方法。

XYZClass *obj=[[XYZClass alloc]init];
NSString *name1=[obj name]; // get 'name'
[obj setName:@"liza"]; // first letter of 'name' becomes capital in setter method
  • List of attributes of @property

    @property的属性列表

    atomic, nonatomic, retain, copy, readonly, readwrite, assign, strong, getter=method, setter=method, unsafe_unretained

    原子,非原子,保留,复制,只读,读写,分配,强,getter=方法,setter=方法,unsafe_unretain。

  • atomic is the default behavior. If an object is declared as atomic then it becomes thread-safe. Thread-safe means, at a time only one thread of a particular instance of that class can have the control over that object.

    原子是默认行为。如果一个对象被声明为原子,那么它就成为线程安全的。线程安全意味着,在同一时间,该类的一个特定实例的一个线程可以控制该对象。

If the thread is performing getter method then other thread cannot perform setter method on that object. It is slow.

如果线程正在执行getter方法,那么其他线程不能在该对象上执行setter方法。它是缓慢的。

@property NSString *name; //by default atomic`
@property (atomic)NSString *name; // explicitly declared atomic`
  • nonatomic is not thread-safe. You can use the nonatomic property attribute to specify that synthesized accessors simply set or return a value directly, with no guarantees about what happens if that same value is accessed simultaneously from different threads.
  • 原子并不是线程安全的。您可以使用非原子属性属性来指定合成的访问器直接设置或返回一个值,而不保证相同的值同时从不同的线程访问时会发生什么。

For this reason, it’s faster to access a nonatomic property than an atomic one.

由于这个原因,访问非原子属性比访问原子属性要快。

@property (nonatomic)NSString *name;   
  • retain is required when the attribute is a pointer to an object.
  • 当属性是指向对象的指针时,需要retain。

The setter method will increase retain count of the object, so that it will occupy memory in autorelease pool.

setter方法将增加对象的保留计数,因此它将占用自动存储池中的内存。

@property (retain)NSString *name;
  • copy If you use copy, you can't use retain. Using copy instance of the class will contain its own copy.
  • 复制如果你使用复制,你不能使用保留。使用类的副本实例将包含它自己的副本。

Even if a mutable string is set and subsequently changed, the instance captures whatever value it has at the time it is set. No setter and getter methods will be synthesized.

即使设置了可变字符串并随后进行了更改,实例也会捕获它在设置时的任何值。不会合成任何setter和getter方法。

@property (copy) NSString *name;

now,

现在,

NSMutableString *nameString = [NSMutableString stringWithString:@"Liza"];    
xyzObj.name = nameString;    
[nameString appendString:@"Pizza"]; 

name will remain unaffected.

名称将不受影响。

  • readonly If you don't want to allow the property to be changed via setter method, you can declare the property readonly.
  • 如果不希望通过setter方法更改属性,则可以声明属性readonly。

Compiler will generate a getter, but not a setter.

编译器将生成一个getter,而不是setter。

@property (readonly) NSString *name;
  • readwrite is the default behavior. You don't need to specify readwrite attribute explicitly.
  • 读写是默认行为。您不需要显式地指定readwrite属性。

It is opposite of readonly.

它与只读相反。

@property (readwrite) NSString *name;
  • assign will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates.
  • assign将生成一个setter,该setter将直接将值分配给实例变量,而不是复制或保留它。这对于NSInteger和CGFloat之类的原始类型或不直接拥有的对象(如委托)是最好的。

Keep in mind retain and assign are basically interchangeable when garbage collection is enabled.

记住,在启用垃圾收集时,retain和assign基本上是可互换的。

@property (assign) NSInteger year;
  • strong is a replacement for retain.
  • 强是保留的替代品。

It comes with ARC.

它有弧。

@property (nonatomic, strong) AVPlayer *player; 
  • getter=method If you want to use a different name for a getter method, it’s possible to specify a custom name by adding attributes to the property.
  • 如果您想为getter方法使用不同的名称,则可以通过向属性添加属性来指定自定义名称。

In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start with the word “is”

对于布尔属性(具有YES或NO值的属性),getter方法通常以“is”开头

@property (getter=isFinished) BOOL finished;
  • setter=method If you want to use a different name for a setter method, it’s possible to specify a custom name by adding attributes to the property.
  • setter=方法如果希望为setter方法使用不同的名称,可以通过向属性添加属性来指定自定义名称。

The method should end with a colon.

这个方法应该以冒号结束。

@property(setter = boolBool:) BOOL finished;
  • unsafe_unretained There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextView, NSFont and NSColorSpace,etc. If you need to use a weak reference to one of these classes, you must use an unsafe reference.
  • Cocoa和Cocoa Touch中有一些类还不支持弱引用,这意味着你不能声明一个弱属性或弱局部变量来跟踪它们。这些类包括NSTextView、NSFont和NSColorSpace等。如果需要对其中一个类使用弱引用,则必须使用不安全引用。

An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated.

不安全引用与弱引用类似,因为它不会使相关对象保持活动状态,但如果目标对象被释放,则不会将其设置为nil。

@property (unsafe_unretained) NSObject *unsafeProperty;

If you need to specify multiple attributes, simply include them as a comma-separated list, like this:

如果需要指定多个属性,只需将它们包含为逗号分隔的列表,如下所示:

@property (readonly, getter=isFinished) BOOL finished;

#3


134  

After reading many articles I decided to put all the attributes information together:

在阅读了很多文章之后,我决定把所有的属性信息放在一起:

  1. atomic //default
  2. 原子/ /默认
  3. nonatomic
  4. 原子
  5. strong=retain //default
  6. 强=保留/ /违约
  7. weak= unsafe_unretained
  8. 弱= unsafe_unretained
  9. retain
  10. 保留
  11. assign //default
  12. 分配/ /默认
  13. unsafe_unretained
  14. unsafe_unretained
  15. copy
  16. 复制
  17. readonly
  18. 只读的
  19. readwrite //default
  20. 读写/ /默认

Below is a link to the detailed article where you can find these attributes.

下面是详细文章的链接,您可以在其中找到这些属性。

Many thanks to all the people who give best answers here!!

非常感谢所有在这里给出最好答案的人!!

Variable property attributes or Modifiers in iOS

iOS中的变量属性或修饰符

Here is the Sample Description from Article

这是文章的示例描述

  1. atomic -Atomic means only one thread access the variable(static type). -Atomic is thread safe. -but it is slow in performance -atomic is default behavior -Atomic accessors in a non garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value. -it is not actually a keyword.
  2. 原子-原子意味着只有一个线程访问变量(静态类型)。化是线程安全的。- -原子是默认行为- - -非垃圾收集环境中的原子访问器(例如使用retain/release/autorelease)将使用一个锁来确保另一个线程不会干扰值的正确设置/获取。-实际上不是关键字。

Example :

例子:

@property (retain) NSString *name;

@synthesize name;
  1. nonatomic -Nonatomic means multiple thread access the variable(dynamic type). -Nonatomic is thread unsafe. -but it is fast in performance -Nonatomic is NOT default behavior,we need to add nonatomic keyword in property attribute. -it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
  2. 非原子-非原子意味着多个线程访问变量(动态类型)。原子是线程不安全的。-非原子性不是默认行为,我们需要在属性中添加非原子性关键字。它可能导致意外的行为,当两个不同的进程(线程)同时访问同一个变量时。

Example:

例子:

@property (nonatomic, retain) NSString *name;

@synthesize name;

Explain:

解释:

Suppose there is an atomic string property called "name", and if you call [self setName:@"A"] from thread A, call [self setName:@"B"] from thread B, and call [self name] from thread C, then all operation on different thread will be performed serially which means if one thread is executing setter or getter, then other threads will wait. This makes property "name" read/write safe but if another thread D calls [name release] simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC) but not thread safe as another threads can simultaneously send any type of messages to the object. Developer should ensure thread safety for such objects.

假设有一个原子字符串属性称为“名称”,如果你叫(自我setName:@“A”)从线程,叫(自我setName:@“B”)从线程B,C并从线程调用(自己的名字),然后在不同的线程将执行所有操作连续这意味着如果一个线程正在执行setter和getter,那么其他线程会等待。这使得属性“name”读/写是安全的,但是如果另一个线程D同时调用[name release],那么这个操作可能会导致崩溃,因为这里不涉及setter/getter调用。这意味着对象是读/写安全的(原子的),而不是线程安全的,因为其他线程可以同时向对象发送任何类型的消息。开发人员应确保此类对象的线程安全。

If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. In case of atomic, Either one of A, B or C will execute first but D can still execute in parallel.

如果属性“name”是非原子的,那么上面示例中的所有线程——A、B、C和D将同时执行,从而产生任何不可预知的结果。在原子的情况下,A、B或C中的任何一个会先执行,但D仍然可以并行执行。

  1. strong (iOS4 = retain ) -it says "keep this in the heap until I don't point to it anymore" -in other words " I'am the owner, you cannot dealloc this before aim fine with that same as retain" -You use strong only if you need to retain the object. -By default all instance variables and local variables are strong pointers. -We generally use strong for UIViewControllers (UI item's parents) -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.
  2. strong (iOS4 = retain)——它说“把这个保存在堆中,直到我不再指向它为止”——换句话说,“我是所有者,您不能在正确地设置为retain之前释放它”——只有在需要保留对象时才使用strong。-默认情况下,所有实例变量和局部变量都是强指针。-我们通常使用强大的UIViewControllers (UI项目的父母)-strong被用于ARC,它基本上可以帮助你,不用担心一个对象的保留计数。当您完成它时,ARC会自动为您释放它。使用强大的关键字表示您拥有该对象。

Example:

例子:

@property (strong, nonatomic) ViewController *viewController;

@synthesize viewController;
  1. weak (iOS4 = unsafe_unretained ) -it says "keep this as long as someone else points to it strongly" -the same thing as assign, no retain or release -A "weak" reference is a reference that you do not retain. -We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only needs to exist as long as the parent object does. -a weak reference is a reference that does not protect the referenced object from collection by a garbage collector. -Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil
  2. -我们通常对iboutlet (UIViewController的Childs)使用弱。这之所以有效,是因为子对象只要父对象存在就可以。-弱引用是不保护被引用对象不受垃圾收集器收集的引用。弱本质上是赋值,一个未保留属性。除了对象被释放时,弱指针被自动设置为nil

Example :

例子:

@property (weak, nonatomic) IBOutlet UIButton *myButton;

@synthesize myButton;

Strong & Weak Explanation, Thanks to BJ Homer:

强和弱的解释,感谢BJ荷马:

Imagine our object is a dog, and that the dog wants to run away (be deallocated). Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached. Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it. As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out. When we use weak? The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).

假设我们的对象是一只狗,而狗想要逃跑(被释放)。强指针就像狗的皮带。只要你把皮带系在狗身上,狗就不会跑掉。如果五个人把他们的皮带拴在一条狗身上,(五根有力的指针指向一个物体),那么狗就不会跑开,直到所有的五根皮带都被解开。另一方面,弱指针就像小孩子指着狗说“看!一只狗!”只要狗还在皮带上,孩子们还能看到狗,他们还会指着它。然而,一旦所有的皮带都被解开,不管有多少小孩子指着它,狗还是跑开了。一旦最后一个强指针(皮带)不再指向一个对象,对象将被释放,所有弱指针将被调零。当我们使用弱吗?你唯一想要使用弱的时候,是如果你想避免保留周期(例如,父母保留了孩子,而孩子保留了父母,所以两者都不会被释放)。

  1. retain = strong -it is retained, old value is released and it is assigned -retain specifies the new value should be sent -retain on assignment and the old value sent -release -retain is the same as strong. -apple says if you write retain it will auto converted/work like strong only. -methods like "alloc" include an implicit "retain"
  2. retain = strong -被保留,旧值被释放,它被分配-retain指定新的值应该被发送-retain on赋值,而发送-release -retain的旧值与strong相同。苹果说,如果你写保留,它将自动转换/只像强。-诸如“alloc”之类的方法包括隐性的“retain”

Example:

例子:

@property (nonatomic, retain) NSString *name;

@synthesize name;
  1. assign -assign is the default and simply performs a variable assignment -assign is a property attribute that tells the compiler how to synthesize the property's setter implementation -I would use assign for C primitive properties and weak for weak references to Objective-C objects.
  2. 分配-分配是默认的,简单地执行一个变量赋值-赋值是一个属性属性,它告诉编译器如何合成属性的setter实现——我将使用赋值给C原始属性,并且弱于对Objective-C对象的弱引用。

Example:

例子:

@property (nonatomic, assign) NSString *address;

@synthesize address;
  1. unsafe_unretained

    unsafe_unretained

    -unsafe_unretained is an ownership qualifier that tells ARC how to insert retain/release calls -unsafe_unretained is the ARC version of assign.

    -unsafe_unretain是一个所有权限定符,它告诉ARC如何插入retain/release调用——unsafe_unretain是assign的ARC版本。

Example:

例子:

@property (nonatomic, unsafe_unretained) NSString *nickName;

@synthesize nickName;
  1. copy -copy is required when the object is mutable. -copy specifies the new value should be sent -copy on assignment and the old value sent -release. -copy is like retain returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments. -if you use copy then you still need to release that in dealloc. -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.
  2. 当对象是可变的时,需要复制。-copy指定应该发送的新值-copy on assignment和发送的旧值-release。-copy就像retain返回一个对象,您必须在非垃圾收集的环境中显式地释放(例如,在dealloc中)。-如果你用的是copy,那你还是需要在dealloc中释放。-如果您需要当前对象的值,并且不希望该值反映该对象的其他所有者所做的任何更改,请使用该值。当您完成该对象时,您将需要释放该对象,因为您将保留该副本。

Example:

例子:

@property (nonatomic, copy) NSArray *myArray;

@synthesize myArray;

#4


8  

Atomic property can be accessed by only one thread at a time. It is thread safe. Default is atomic .Please note that there is no keyword atomic

一次只能被一个线程访问原子属性。它是线程安全的。默认是原子的。请注意没有关键字原子

Nonatomic means multiple thread can access the item .It is thread unsafe

非原子性意味着多个线程可以访问该项目,它是线程不安全的。

So one should be very careful while using atomic .As it affect the performance of your code

因此,在使用atomic .As它影响代码的性能时应该非常小心

#1


263  

The article linked to by MrMage is no longer working. So, here is what I've learned in my (very) short time coding in Objective-C:

由MrMage链接的文章不再有效。这是我在Objective-C中(非常)短时间编码学到的东西:

nonatomic vs. atomic - "atomic" is the default. Always use "nonatomic". I don't know why, but the book I read said there is "rarely a reason" to use "atomic". (BTW: The book I read is the BNR "iOS Programming" book.)

非原子vs原子-“原子”是默认值。总是使用“原子”。我不知道为什么,但我读过的那本书说“很少有理由”使用“原子”。(顺便说一句:我读的书是BNR的《iOS编程》。)

readwrite vs. readonly - "readwrite" is the default. When you @synthesize, both a getter and a setter will be created for you. If you use "readonly", no setter will be created. Use it for a value you don't want to ever change after the instantiation of the object.

readwrite vs. readonly -“readwrite”是默认值。当您@synthesize时,会为您创建一个getter和setter。如果使用“readonly”,则不会创建任何setter。将它用于对象实例化后不希望更改的值。

retain vs. copy vs. assign

保留、复制、分配

  • "assign" is the default. In the setter that is created by @synthesize, the value will simply be assigned to the attribute. My understanding is that "assign" should be used for non-pointer attributes.
  • “分配”是默认的。在@synthesize创建的setter中,只需将值分配给属性。我的理解是“assign”应该用于非指针属性。
  • "retain" is needed when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count) the object. You will need to release the object when you are finished with it.
  • 当属性是指向对象的指针时,需要“retain”。@synthesize生成的setter将保留(也称为添加保留计数)对象。当您完成该对象时,您将需要释放它。
  • "copy" is needed 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.
  • 当对象是可变的时,需要“复制”。如果您需要此时对象的值,并且不希望该值反映对象的其他所有者所做的任何更改,那么请使用该值。当您完成该对象时,您将需要释放该对象,因为您将保留该副本。

#2


270  

Before you know about the attributes of @property, you should know what is the use of @property.

在您了解@property的属性之前,您应该了解@property的用途。

  • @property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.

    @property提供了一种定义类要封装的信息的方法。如果您使用@property声明一个对象/变量,那么该对象/变量将被导入其类的其他类访问。

  • If you declare an object using @property in the header file, then you have to synthesize it using @synthesize in the implementation file. This makes the object KVC compliant. By default, compiler will synthesize accessor methods for this object.

    如果在头文件中使用@property声明一个对象,那么必须在实现文件中使用@synthesize合成它。这使得对象KVC兼容。默认情况下,编译器将为该对象合成访问器方法。

  • accessor methods are : setter and getter.

    访问器方法是:setter和getter。

Example: .h

例子:. h

@interface XYZClass : NSObject
@property (nonatomic, retain) NSString *name;
@end

.m

00

@implementation XYZClass
@synthesize name;
@end

Now the compiler will synthesize accessor methods for name.

现在编译器将为名称合成访问器方法。

XYZClass *obj=[[XYZClass alloc]init];
NSString *name1=[obj name]; // get 'name'
[obj setName:@"liza"]; // first letter of 'name' becomes capital in setter method
  • List of attributes of @property

    @property的属性列表

    atomic, nonatomic, retain, copy, readonly, readwrite, assign, strong, getter=method, setter=method, unsafe_unretained

    原子,非原子,保留,复制,只读,读写,分配,强,getter=方法,setter=方法,unsafe_unretain。

  • atomic is the default behavior. If an object is declared as atomic then it becomes thread-safe. Thread-safe means, at a time only one thread of a particular instance of that class can have the control over that object.

    原子是默认行为。如果一个对象被声明为原子,那么它就成为线程安全的。线程安全意味着,在同一时间,该类的一个特定实例的一个线程可以控制该对象。

If the thread is performing getter method then other thread cannot perform setter method on that object. It is slow.

如果线程正在执行getter方法,那么其他线程不能在该对象上执行setter方法。它是缓慢的。

@property NSString *name; //by default atomic`
@property (atomic)NSString *name; // explicitly declared atomic`
  • nonatomic is not thread-safe. You can use the nonatomic property attribute to specify that synthesized accessors simply set or return a value directly, with no guarantees about what happens if that same value is accessed simultaneously from different threads.
  • 原子并不是线程安全的。您可以使用非原子属性属性来指定合成的访问器直接设置或返回一个值,而不保证相同的值同时从不同的线程访问时会发生什么。

For this reason, it’s faster to access a nonatomic property than an atomic one.

由于这个原因,访问非原子属性比访问原子属性要快。

@property (nonatomic)NSString *name;   
  • retain is required when the attribute is a pointer to an object.
  • 当属性是指向对象的指针时,需要retain。

The setter method will increase retain count of the object, so that it will occupy memory in autorelease pool.

setter方法将增加对象的保留计数,因此它将占用自动存储池中的内存。

@property (retain)NSString *name;
  • copy If you use copy, you can't use retain. Using copy instance of the class will contain its own copy.
  • 复制如果你使用复制,你不能使用保留。使用类的副本实例将包含它自己的副本。

Even if a mutable string is set and subsequently changed, the instance captures whatever value it has at the time it is set. No setter and getter methods will be synthesized.

即使设置了可变字符串并随后进行了更改,实例也会捕获它在设置时的任何值。不会合成任何setter和getter方法。

@property (copy) NSString *name;

now,

现在,

NSMutableString *nameString = [NSMutableString stringWithString:@"Liza"];    
xyzObj.name = nameString;    
[nameString appendString:@"Pizza"]; 

name will remain unaffected.

名称将不受影响。

  • readonly If you don't want to allow the property to be changed via setter method, you can declare the property readonly.
  • 如果不希望通过setter方法更改属性,则可以声明属性readonly。

Compiler will generate a getter, but not a setter.

编译器将生成一个getter,而不是setter。

@property (readonly) NSString *name;
  • readwrite is the default behavior. You don't need to specify readwrite attribute explicitly.
  • 读写是默认行为。您不需要显式地指定readwrite属性。

It is opposite of readonly.

它与只读相反。

@property (readwrite) NSString *name;
  • assign will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates.
  • assign将生成一个setter,该setter将直接将值分配给实例变量,而不是复制或保留它。这对于NSInteger和CGFloat之类的原始类型或不直接拥有的对象(如委托)是最好的。

Keep in mind retain and assign are basically interchangeable when garbage collection is enabled.

记住,在启用垃圾收集时,retain和assign基本上是可互换的。

@property (assign) NSInteger year;
  • strong is a replacement for retain.
  • 强是保留的替代品。

It comes with ARC.

它有弧。

@property (nonatomic, strong) AVPlayer *player; 
  • getter=method If you want to use a different name for a getter method, it’s possible to specify a custom name by adding attributes to the property.
  • 如果您想为getter方法使用不同的名称,则可以通过向属性添加属性来指定自定义名称。

In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start with the word “is”

对于布尔属性(具有YES或NO值的属性),getter方法通常以“is”开头

@property (getter=isFinished) BOOL finished;
  • setter=method If you want to use a different name for a setter method, it’s possible to specify a custom name by adding attributes to the property.
  • setter=方法如果希望为setter方法使用不同的名称,可以通过向属性添加属性来指定自定义名称。

The method should end with a colon.

这个方法应该以冒号结束。

@property(setter = boolBool:) BOOL finished;
  • unsafe_unretained There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextView, NSFont and NSColorSpace,etc. If you need to use a weak reference to one of these classes, you must use an unsafe reference.
  • Cocoa和Cocoa Touch中有一些类还不支持弱引用,这意味着你不能声明一个弱属性或弱局部变量来跟踪它们。这些类包括NSTextView、NSFont和NSColorSpace等。如果需要对其中一个类使用弱引用,则必须使用不安全引用。

An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated.

不安全引用与弱引用类似,因为它不会使相关对象保持活动状态,但如果目标对象被释放,则不会将其设置为nil。

@property (unsafe_unretained) NSObject *unsafeProperty;

If you need to specify multiple attributes, simply include them as a comma-separated list, like this:

如果需要指定多个属性,只需将它们包含为逗号分隔的列表,如下所示:

@property (readonly, getter=isFinished) BOOL finished;

#3


134  

After reading many articles I decided to put all the attributes information together:

在阅读了很多文章之后,我决定把所有的属性信息放在一起:

  1. atomic //default
  2. 原子/ /默认
  3. nonatomic
  4. 原子
  5. strong=retain //default
  6. 强=保留/ /违约
  7. weak= unsafe_unretained
  8. 弱= unsafe_unretained
  9. retain
  10. 保留
  11. assign //default
  12. 分配/ /默认
  13. unsafe_unretained
  14. unsafe_unretained
  15. copy
  16. 复制
  17. readonly
  18. 只读的
  19. readwrite //default
  20. 读写/ /默认

Below is a link to the detailed article where you can find these attributes.

下面是详细文章的链接,您可以在其中找到这些属性。

Many thanks to all the people who give best answers here!!

非常感谢所有在这里给出最好答案的人!!

Variable property attributes or Modifiers in iOS

iOS中的变量属性或修饰符

Here is the Sample Description from Article

这是文章的示例描述

  1. atomic -Atomic means only one thread access the variable(static type). -Atomic is thread safe. -but it is slow in performance -atomic is default behavior -Atomic accessors in a non garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value. -it is not actually a keyword.
  2. 原子-原子意味着只有一个线程访问变量(静态类型)。化是线程安全的。- -原子是默认行为- - -非垃圾收集环境中的原子访问器(例如使用retain/release/autorelease)将使用一个锁来确保另一个线程不会干扰值的正确设置/获取。-实际上不是关键字。

Example :

例子:

@property (retain) NSString *name;

@synthesize name;
  1. nonatomic -Nonatomic means multiple thread access the variable(dynamic type). -Nonatomic is thread unsafe. -but it is fast in performance -Nonatomic is NOT default behavior,we need to add nonatomic keyword in property attribute. -it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
  2. 非原子-非原子意味着多个线程访问变量(动态类型)。原子是线程不安全的。-非原子性不是默认行为,我们需要在属性中添加非原子性关键字。它可能导致意外的行为,当两个不同的进程(线程)同时访问同一个变量时。

Example:

例子:

@property (nonatomic, retain) NSString *name;

@synthesize name;

Explain:

解释:

Suppose there is an atomic string property called "name", and if you call [self setName:@"A"] from thread A, call [self setName:@"B"] from thread B, and call [self name] from thread C, then all operation on different thread will be performed serially which means if one thread is executing setter or getter, then other threads will wait. This makes property "name" read/write safe but if another thread D calls [name release] simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC) but not thread safe as another threads can simultaneously send any type of messages to the object. Developer should ensure thread safety for such objects.

假设有一个原子字符串属性称为“名称”,如果你叫(自我setName:@“A”)从线程,叫(自我setName:@“B”)从线程B,C并从线程调用(自己的名字),然后在不同的线程将执行所有操作连续这意味着如果一个线程正在执行setter和getter,那么其他线程会等待。这使得属性“name”读/写是安全的,但是如果另一个线程D同时调用[name release],那么这个操作可能会导致崩溃,因为这里不涉及setter/getter调用。这意味着对象是读/写安全的(原子的),而不是线程安全的,因为其他线程可以同时向对象发送任何类型的消息。开发人员应确保此类对象的线程安全。

If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. In case of atomic, Either one of A, B or C will execute first but D can still execute in parallel.

如果属性“name”是非原子的,那么上面示例中的所有线程——A、B、C和D将同时执行,从而产生任何不可预知的结果。在原子的情况下,A、B或C中的任何一个会先执行,但D仍然可以并行执行。

  1. strong (iOS4 = retain ) -it says "keep this in the heap until I don't point to it anymore" -in other words " I'am the owner, you cannot dealloc this before aim fine with that same as retain" -You use strong only if you need to retain the object. -By default all instance variables and local variables are strong pointers. -We generally use strong for UIViewControllers (UI item's parents) -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.
  2. strong (iOS4 = retain)——它说“把这个保存在堆中,直到我不再指向它为止”——换句话说,“我是所有者,您不能在正确地设置为retain之前释放它”——只有在需要保留对象时才使用strong。-默认情况下,所有实例变量和局部变量都是强指针。-我们通常使用强大的UIViewControllers (UI项目的父母)-strong被用于ARC,它基本上可以帮助你,不用担心一个对象的保留计数。当您完成它时,ARC会自动为您释放它。使用强大的关键字表示您拥有该对象。

Example:

例子:

@property (strong, nonatomic) ViewController *viewController;

@synthesize viewController;
  1. weak (iOS4 = unsafe_unretained ) -it says "keep this as long as someone else points to it strongly" -the same thing as assign, no retain or release -A "weak" reference is a reference that you do not retain. -We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only needs to exist as long as the parent object does. -a weak reference is a reference that does not protect the referenced object from collection by a garbage collector. -Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil
  2. -我们通常对iboutlet (UIViewController的Childs)使用弱。这之所以有效,是因为子对象只要父对象存在就可以。-弱引用是不保护被引用对象不受垃圾收集器收集的引用。弱本质上是赋值,一个未保留属性。除了对象被释放时,弱指针被自动设置为nil

Example :

例子:

@property (weak, nonatomic) IBOutlet UIButton *myButton;

@synthesize myButton;

Strong & Weak Explanation, Thanks to BJ Homer:

强和弱的解释,感谢BJ荷马:

Imagine our object is a dog, and that the dog wants to run away (be deallocated). Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached. Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it. As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out. When we use weak? The only time you would want to use weak, is if you wanted to avoid retain cycles (e.g. the parent retains the child and the child retains the parent so neither is ever released).

假设我们的对象是一只狗,而狗想要逃跑(被释放)。强指针就像狗的皮带。只要你把皮带系在狗身上,狗就不会跑掉。如果五个人把他们的皮带拴在一条狗身上,(五根有力的指针指向一个物体),那么狗就不会跑开,直到所有的五根皮带都被解开。另一方面,弱指针就像小孩子指着狗说“看!一只狗!”只要狗还在皮带上,孩子们还能看到狗,他们还会指着它。然而,一旦所有的皮带都被解开,不管有多少小孩子指着它,狗还是跑开了。一旦最后一个强指针(皮带)不再指向一个对象,对象将被释放,所有弱指针将被调零。当我们使用弱吗?你唯一想要使用弱的时候,是如果你想避免保留周期(例如,父母保留了孩子,而孩子保留了父母,所以两者都不会被释放)。

  1. retain = strong -it is retained, old value is released and it is assigned -retain specifies the new value should be sent -retain on assignment and the old value sent -release -retain is the same as strong. -apple says if you write retain it will auto converted/work like strong only. -methods like "alloc" include an implicit "retain"
  2. retain = strong -被保留,旧值被释放,它被分配-retain指定新的值应该被发送-retain on赋值,而发送-release -retain的旧值与strong相同。苹果说,如果你写保留,它将自动转换/只像强。-诸如“alloc”之类的方法包括隐性的“retain”

Example:

例子:

@property (nonatomic, retain) NSString *name;

@synthesize name;
  1. assign -assign is the default and simply performs a variable assignment -assign is a property attribute that tells the compiler how to synthesize the property's setter implementation -I would use assign for C primitive properties and weak for weak references to Objective-C objects.
  2. 分配-分配是默认的,简单地执行一个变量赋值-赋值是一个属性属性,它告诉编译器如何合成属性的setter实现——我将使用赋值给C原始属性,并且弱于对Objective-C对象的弱引用。

Example:

例子:

@property (nonatomic, assign) NSString *address;

@synthesize address;
  1. unsafe_unretained

    unsafe_unretained

    -unsafe_unretained is an ownership qualifier that tells ARC how to insert retain/release calls -unsafe_unretained is the ARC version of assign.

    -unsafe_unretain是一个所有权限定符,它告诉ARC如何插入retain/release调用——unsafe_unretain是assign的ARC版本。

Example:

例子:

@property (nonatomic, unsafe_unretained) NSString *nickName;

@synthesize nickName;
  1. copy -copy is required when the object is mutable. -copy specifies the new value should be sent -copy on assignment and the old value sent -release. -copy is like retain returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments. -if you use copy then you still need to release that in dealloc. -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.
  2. 当对象是可变的时,需要复制。-copy指定应该发送的新值-copy on assignment和发送的旧值-release。-copy就像retain返回一个对象,您必须在非垃圾收集的环境中显式地释放(例如,在dealloc中)。-如果你用的是copy,那你还是需要在dealloc中释放。-如果您需要当前对象的值,并且不希望该值反映该对象的其他所有者所做的任何更改,请使用该值。当您完成该对象时,您将需要释放该对象,因为您将保留该副本。

Example:

例子:

@property (nonatomic, copy) NSArray *myArray;

@synthesize myArray;

#4


8  

Atomic property can be accessed by only one thread at a time. It is thread safe. Default is atomic .Please note that there is no keyword atomic

一次只能被一个线程访问原子属性。它是线程安全的。默认是原子的。请注意没有关键字原子

Nonatomic means multiple thread can access the item .It is thread unsafe

非原子性意味着多个线程可以访问该项目,它是线程不安全的。

So one should be very careful while using atomic .As it affect the performance of your code

因此,在使用atomic .As它影响代码的性能时应该非常小心