The IOS property attribute details the usage considerations

  • 2021-12-04 11:28:20
  • OfStack

IOS property Properties

Atomicity

atomic nonatomic

Access rights

readWrite readOnly

Memory management

strong retain copy assign weak unsafe_unretained

Method naming

getter = < name > setter = < name >

Conclusion

1. Atomicity

(1) atomic

Atomicity, it can be understood that when generating getter and setter methods, the compiler will automatically add synchronization locks to the program, thus reducing the 2-meaning and data confusion caused by reading and writing the same memory address at the same time in different threads.

However, synchronous lock can not guarantee that it can completely prevent simultaneous reading and writing, and synchronous lock will consume more resources and reduce the efficiency of programs, so it is not recommended to use, but atomic is the default feature.

(2) nonatomic

Non-atomicity, that is, without adding synchronization lock, the reading and writing efficiency will be high, but developers need to pay attention to the situation of reading and writing at the same time, which can be guaranteed by using the fence and synchronization method of gcd.

2. Access rights

(1) readWrite

Generate getter, setter methods, and generate instance variable pointers, but for static library classes can not be modified, so even if you can enter the header file can not be modified.

(2) readOnly

Only the getter method can be used for data protection. In interface, the attribute is declared as readOnly, and in continue-class, the attribute is declared as readWrite, which can realize the effect of private attribute and thus have better encapsulation characteristics. But in fact, through KVC

Properties can still be modified externally, but this is not recommended.

3. Memory management

(1) strong: strong is an attribute added after deducing the automatic counting of ARC, and its effect is basically the same as that of retain. Both of them are in setter method, for the old value release and the new value retain.
(2) retain and strong are basically identical
(3) assign: Simple assignment operation, usually for common types, neither release nor retain
(4) copy: It can better protect the context environment. For the old value release, copy operation is performed for the new value, immutable+copy = immutable shallow copy, mutable+copy = immutable deep copy.
(5) weak: weak feature, does not hold an object, when the pointer to the object is completely revoked, the object is released, and the pointer is set to nil, which is commonly used in the reserved ring of block
(6) unsafe_unretained: It does not hold an object, but refers to it. After the object is released, it will produce a wild pointer, which is mainly used for compatibility with previous methods.

4. Method naming

(1) getter = < name >

Default getter method name for property generation: property name

(2) setter = < name >

Default getter method name for property generation: set + property name

5. Conclusion

Attribute is not filled in casually, and it needs to be selected strictly according to its role in the program.

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


Related articles: