This is mainly done before iOS development, write technical articles, now moved to the nuggets above

We have built an interview question solution project, you can have a look, I hope you can help to give a STAR, thank you!

Project address: github.com/NotFound9/i…

In daily iOS development, we will use property every day. However, we may not understand the property modifier thoroughly as I did before. Now let’s understand it.

First, a property can be thought of as:

Property = instance variable +setter method +getter method

Property modifiers directly affect subsequent compilations of setter and getter methods. Property modifiers fall into the following three categories:

1. The atomicity

atomic VS nonatomic

If the modifier is nonatomic, the setter and getter methods are not locked. In general, if the modifier is nonatomic, the setter and getter methods are not locked. Using nonatomic is not a problem, but if one thread is modifying a property multiple times and another thread is reading the property, it may fetch an unmodified property. Here’s an example:





Modifying dataArray with nonatomic will crash





DataArray will not crash with atomic modifications

In this code, we have an array dataArray decorated with nonatomic strong, and we keep modifying that property in one asynchronous queue task, and we keep reading that property in another asynchronous queue task, and since dataArray is decorated with strong, Therefore, in the setter method of dataArray, the new value is reserved first, then the old value is released, and then the pointer points to the new value. Therefore, in the task of the asynchronous queue, the dataArray obtained may be a zombie object that has been released, so it will crash.

So there’s a problem

Are Atomic thread-safe?

Let’s look at the concept of thread safety first,

Multithreaded operations that share data without unintended consequences are thread-safe, otherwise, thread-unsafe.

So let’s do a little experiment,




Read properties for successive modifications to an atomic decorated array

The output is:




The output

We wrote dataArray for many times in one asynchronous queue and read dataArray for many times in another asynchronous queue. We found that the output results were different, so using atomic could not guarantee thread safety.

2. Read/write permission

readwrite VS readonly

Properties modified by readwrite synthesize only setter and getter methods

A readonly modified property synthesizes only getter methods and can be assigned by directly accessing the instance variable

3. Memory management

The main thing about this modifier is that it affects the compiler to synthesize setter methods,

assign

The setter methods for attributes that use the Assign modifier mainly perform simple assignment operations

weak

Weak is a new property modifier added to ARC, which defines a “non-ownership relationship”. Setter methods using the weak modifier will neither retain the new value nor release the old value, will not increase the reference count of the object referred to by the property, and will automatically set the property value to nil when the object referred to is released.

strong

Strong is a new property modifier added to ARC. Much like the retain modifier from the MRC era, strong describes an “ownership relationship” in which setter methods that use the strong modifier retain the new value, release the old value, and finally set the new value.

unsafe_unretained

Unsafe_unretained assign is similar to that of an object type, but it is not secure and does not strongly refer to an object. Weak is very similar to unsafe_unretained assign, but weak is not secure as it does not set to nil when the directed object is released.

copy

Copy is semantically similar in that it holds the object, but instead of keeping the new value, its setter calls the copy method of the object to make a copy of the new value, and then sets it, so any type that uses copy must comply with the NSCopying protocol. -(void)copyWithZone:(NSZone *)zone