Detailed explanation of property attributes in OC (assign, retain, copy, strong, weak, readonly, readwrite, atomic, nonatomic)

Objective-C property characteristics @property detailed explanation   

table of Contents

Objective-C property characteristics @property detailed explanation     

1.assign

2.retain

3.copy

4.strong

5.weak

6.readonly

7.readwrite

8.atomic

9.nonatomic

1.assign

The setter method assigns incoming parameters to instance variables. When only setting variables, assign applies to basic data types and is a weak reference. In fact, assign can also be used to modify objects. So why don't we use it to decorate objects? Because the object modified by assign (generally generates a warning when compiling: Assigning retained object to unsafe property; object will be released after assignment) after the release, the pointer address still exists, that is to say, the pointer is not set to nil , Resulting in wild pointers. Objects are generally allocated to a certain block of memory on the heap. If this address happens to be allocated in subsequent memory allocations, the program will crash.

Then why can assign basic data types be modified? Because the basic data types are generally allocated on the stack, the memory of the stack will be automatically processed by the system itself and will not cause wild pointers.

2.retain

Indicates holding characteristics, the setter method will first retain the incoming parameters, and then assign values, the retaincount of the incoming parameters will be +1; 

3.copy

 Represents the copy feature. The setter method copies the incoming object in memory, and the two pointers point to different memory addresses. When a new variable is required. In MRC, this is done to release the old object (the reference counter of the old object -1), copy the new object (the reference counter of the new object +1), and then point to the new object (the new object refers to the object eventually pointed to, no matter how deep Copy or shallow copy). In ARC, copy the new object (the reference counter of the new object + 1), and then point to the new object.

Note  : For example, NSMutableArray is decorated with copy, adding elements can crash as soon as it runs, because after copy, it actually becomes NSArray. Then, mutablecopy is needed at this time. Use of objects that comply with the NSCopying protocol.

4.strong

A strong reference will make the reference counter +1, and the two pointers point to the same memory address. Under ARC (replacing the role of retain), when an object is not pointed by a strong reference, the system will release the object 

5.weak

Weak references will not make the reference counter +1, and the pointer address will be set to nil after the object modified by weak is released. 

Use scenarios for weak:

Under ARC, when there is a possibility of circular references, it is often solved by letting one end of it use weak, for example, the delegate property is usually declared as weak. It has already been strongly quoted once, and weak is also used when there is no need to quote it again. For example: custom IBOutlet control properties generally also use weak, of course, you can also use strong.

6.readonly

Read-only properties, only getter methods are generated, which means that only variables can be accessed, not modified.

7.readwrite

The default properties, readable and writable, generate setter and getter methods. 

8.atomic

Atomicity, the representation of the attribute security level, only one thread can access at the same time, which has resource exclusivity, but the efficiency is very low. Add mutex lock @synchronized (lock object) to the set method. Mutual exclusion locks are implemented by thread synchronization, which is intended to ensure that only one thread calls the set method at the same time. In fact, there is also a get method. If set and get are called at the same time, there will still be problems. So even if the atomic modification is used, it is not safe . 

9.nonatomic

Non-atomic, multi-threaded access, high efficiency, no lock on the set method, good performance, unsafe threads, it seems to be officially recommended by Apple.

 

Guess you like

Origin blog.csdn.net/zjpjay/article/details/86526378