Differences in IOS (assign retain copy weak strong) and Meaning of nonatomic

  • 2021-11-14 07:17:41
  • OfStack

Differences in IOS (assign, retain, copy, weak, strong) and Meaning of nonatomic

When we declare the @ property attribute, we always write one of assign, retain, copy, weak and strong in brackets. Most of the time, we only write the one we often write according to our habits, but sometimes when we look at the code, we will find that others use it differently. What is the difference between these?

First of all, the above five are not at one level, but can be divided into two parts, the first part is assign, retain and copy, and the second part is weak and strong.

Let's start with assign, retain and copy in Part 1.

assign:

assign1 is used to modify basic data types, including basic data types (NSInteger, CGFloat) and C data types (int, float, double, char, etc.). Why? assign declared attributes do not increase the reference count, that is to say, after the declared attributes are released, they are gone. Even if other objects use it, they cannot keep it, only crash. However, even if it is released, the pointer still exists and becomes a wild pointer. If a new object is allocated to this memory address, it will be crash, so 1 is only used to declare basic data types, because they will be allocated to the stack, and the stack will be automatically processed by the system without causing a wild pointer.

retain:

In contrast to assign, we need to use retain to solve the problem caused by releasing objects after being referenced by other objects. The object declared by retain will change the reference count, so every time it is referenced, the reference count will be +1, and after being released, it will be-1. Even if the object itself is released, as long as there are objects referencing it, it will be held without causing any problems. Only when the reference count is 0, it will be reclaimed by dealloc destructor.

copy:

The most common declaration of copy should be NSString. The difference between copy and retain is that the reference of retain is the copy pointer address, while copy is the copy object itself, that is to say, retain is shallow copy and copy is deep copy. If it is shallow copy, when the object value is modified, it will be modified, but deep copy will not. The reason why they are used on objects with variable types such as NSString is that they may be assigned to corresponding variable types such as NSMutableString. In order to prevent the content from being changed, copy is used to make a deep copy. The copy work is performed by the copy method, and this property is only valid for those object types that implement the NSCopying protocol.

The above three can be used in MRC, but weak and strong can only be used in ARC, that is, automatic reference counting. At this time, retain, release and other operations cannot be performed manually, and ARC will help us complete these tasks.

weak:

weak is actually similar to assign, which is called weak reference and does not increase the reference count. 1 is only used to prevent circular references, such as when the parent class refers to a subclass, and the subclass refers to the parent class. IBOutlet, like Delegate1, uses weak because they are called outside the class to prevent circular references.

strong:

In contrast, strong is similar to retain, which is called strong reference, which will increase the reference count. Attribute 1 used inside the class is generally modified by strong. Now ARC has basically replaced MRC, so the most common one is strong.

nonatomic:

When modifying attributes, we often add an nonatomic. What is this? Its name is nonatomic access. The corresponding is atomic, which is atomic access. As we know, when using multithreading, in order to avoid the problem of writing at the same time, the object to be written is often locked, that is, only one thread is allowed to operate it at the same time. If a property is decorated with atomic, the system is thread-protected to prevent multiple writes from occurring simultaneously. This has advantages, but it also has disadvantages, that is, it consumes system resources. Therefore, for small devices like iPhone, if it is not for multi-threaded write operation, nonatomic can be used to cancel thread protection and improve performance.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: