“This is the 20th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The @property function is twofold: it automatically generates private properties (usually underscores + property names), and it automatically generates getters and setters. When a property is declared, the property modifier immediately following @property describes the rules for automatically generating getter and setter methods.
According to the division of MRC and ARC attributes: the scope of application of the modifier MRC: nonatomic, atomic, retain, assign, copy, readwrite, readonly ARC: nonatomic,atomic,strong,weak,assign,copy,readwrite,readonly
-
nonatomic
-
Nonatomic denotes a non-atomic attribute
-
Concurrent access performance is high, but access is insecure
- It accesses the address directly in memory, regardless of whether other threads are changing the value, and there is no deadlock protection in the middle; So you might not get the full value
-
-
-
atomic
-
Atomic means atomic property
-
Getters/setters generated by the system ensure the integrity of get and set operations, which are not affected by other threads
- The getter/setter method generated by the system uses @synchronized(self)
- If one thread is executing a getter/setter, the other thread has to wait
-
If another thread was calling [property Release] at the same time, it might crash because release is not bound by getter/setter operations.
-
That is, the attributes modified by atomic are read/write safe, but not thread-safe
- Because other threads can do more than just read and write.
- Thread safety needs to be ensured by the developers themselves.
-
-
-
The default property is atomic
-
-
strong
-
Strong Indicates a strong reference to an object
- For strong references, the reference count is +1
- When assigning the strong property, setter methods release the old value and retain the new value and assign it
- Strong references between two objects can cause circular references and memory leaks
-
-
weak
-
Weak Indicates a weak reference to an object
-
Weak references do not make the incoming object count +1
-
Objects modified by it can be destroyed and recycled by the system at any time
-
When the reference count of this object is 0, it is reclaimed, and when the object is freed, the weak pointer is automatically set to nil
In the runtime environment of OC, a kind of weak table (hash table) is maintained. This hash table uses the first address of the object as the key and an array of the addresses of the weak pointer itself as the value. When the object is released, the starting address of the object is used to find all the weak Pointers to it. And point them to nil
-
-
Weak is mostly used to avoid circular references
-
-
-
assign
-
Assign is primarily used to modify base data types
- Includes OC basic data types (NSInteger, CGFloat) and C data types (int, float, double, char)
-
Basic data types are stored on the stack, and memory is not managed by the programmer.
-
Assign can also modify objects, but when the object is freed, the pointer still points to the previous memory address. \
-
At this point, access to the freed address will crash
-
This freed object is called a zombie object
-
-
Assign is available under both MRC and ARC
-