Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations.
Atomic
When setting the @ property attribute of a member variable, the default value is atomic, providing multi-thread security.
In a multi-threaded environment, atomic operations are necessary; otherwise, errors may occur. With Atomic added, the setter function will become as follows:
{Lock}
If
(Property! = Newvalue ){
[Property release];
Property = [newvalue retain];
}
{Unlock}
Nonatomic
Multithreading and Variable Protection are prohibited to improve performance.
Atomic is a thread protection technology used by objc. It basically prevents reading data by another thread when the write is not completed, resulting in data errors. This mechanism consumes system resources. Therefore, nonatomic is a good choice for small devices such as the iPhone, if multi-thread communication programming is not used.
It indicates that accessors are not atomic operations, but by default, accessors are atomic operations. That is to say, in a multi-threaded environment, the parser provides a secure access to the attributes. The returned values obtained from the reader or the values set by the setter can be completed at one time, even other threads are accessing it. If you do not specify
Nonatomic: in an environment where you manage the memory, the parsed accessors retain and automatically release the returned values. If nonatomic is specified, the accessors simply return this value.
Assign
For basic data types (nsinteger, cgfloat) and C data types (INT, float, double, char), etc.
This flag indicates that the setter directly assigns a value, which is also the default value. In applications that use garbage collection, if you want an attribute to use assign and the class complies with the nscopying agreement, you need to specify this tag rather than simply using the default value, otherwise, you will get a compilation warning. This again shows to the compiler that you really need to assign values, even if it is copyable.
Retain
Perform the release old value on the parameters of other nsobject and its subclass, and then retain the new value.
The specified retain will wake up the retain message of the input value when the value is assigned. This attribute can only be used for the objective-C object type, but not for the core foundation object. (The reason is obvious: retain will increase the reference count of the object, and the basic data type or core
None of the Foundation Objects Reference count-Translator's note ).
Note: when an object is added to an array, the reference count increases the number of object references by more than 1.
Copy
It indicates to nsstring that a copy of the input value is used when the value is assigned. The copy operation is executed by the copy method. This attribute is only valid for object types that implement the nscopying protocol. For more information, see "copy.
Copy and retain:
Copy actually creates the same object, and retain is not:
1. For example, an nsstring object with the address 0 × 1111, content @ "STR", copy
To another nsstring, the address is 0 × 2222 with the same content.
2. The new object retain is 1, and the old object does not change retain.
After another nsstring, the address is the same (create a pointer and copy the pointer), and the content is of course the same. The retain value of this object is + 1.
Conclusion: retain is a pointer copy and copy is a content copy.
Assign and retain:
1.
If you have been in contact with C, assume that you have allocated a piece of memory with malloc and assigned its address to pointer A. Later, you want pointer B to share the same piece of memory, so you assigned a to (assign) B again. At this time, a and B point to the same memory. Can a directly release this memory when a no longer needs it? The answer is no, because a does not know whether B is still using this memory. If a is released, B will cause crash when using this memory.
2. I learned about the assign problem in 1. How can I solve it? The simplest way is to use reference counting. In the example above, we set a reference count for the memory. When the memory is allocated and assigned to, the reference count is 1. When a is assigned to B, the reference count is increased to 2. If a no longer uses this memory, it only needs to reduce the reference count by 1, indicating that it no longer owns this memory. When B no longer uses this memory, it also reduces the reference count by 1. When the reference count is 0, it indicates that the memory is no longer referenced by any pointer, and the system can release it directly.
Conclusion: The above two points are actually the differences between assign and retain. Assign is a direct value assignment, which may cause problems in 1. When the data is of native types such as int and float, assign can be used. As described in 2, retain uses the reference count. Retain causes the reference count to increase by 1,
Release causes the reference count to be reduced by 1. When the reference count is 0, the dealloc function is called and the memory is recycled.