Source: http://www.2cto.com/kf/201205/133943.html
Assign: simple assignment without changing the index count
Copy: Create an object with an index count of 1 and release the old object.
Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
Copy actually creates the same object, and retain is not:
For example, an nsstring object with the address 0 × 1111 and the content @ "STR"
After copying data to another nsstring, the address is 0 × 2222, the content is the same, the new object retain is 1, and the old object does not change
After retain arrives at 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
That is to say, retain is a pointer copy and copy is a content copy. The old object will be released before the copy.
* Use assign: for basic data types (nsinteger) and C data types (INT, float, double, Char, etc)
* Use copy: For nsstring
* Use retain: for other nsobject and its subclass
1. readonly indicates that this attribute is read-only, that is, only the getter method is generated and the setter method is not generated.
2. readwrite, set the available access level
3. Retain: indicates that when the attribute is assigned a value, the previous value is release first, and then the new value is assigned to the attribute, and the reference is incremented by 1.
4. nonatomic, non-atomic access, without synchronization, multi-thread concurrent access will improve performance. Note: If this attribute is not added, both access methods are atomic transaction access by default.
The difference between retain and copy and assign
1. assume that you allocated a piece of memory with malloc and assigned its address to pointer A. Then you want pointer B to share the block of memory, so you assign a value to assign) b. 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.
3. 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 section 2, retain uses the reference count. Retain causes the reference count to increase by 1, and release causes the reference count to decrease by 1. When the reference count is 0, the dealloc function is called, memory is recycled.
4. Copy is used when you do not want a and B to share a piece of memory. A and B each have their own memory.
5. Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multi-threaded environment, atomic operations are necessary; otherwise, errors may occur. With Atomic added, the setter function will become as follows:
If (property! = Newvalue ){
[Property release];
Property = [newvalue retain];
}
The difference between retain, copy, and assign has actually plagued me for a long time, because it is not very common in the program to copy and assign, so the specific differences between the three have not been quite clear.
According to my understanding, the difference between assign and retain is that a counter retaincount is introduced to facilitate the release of a memory. Copy is to copy the original memory so that each of them has a memory, so that there will be no error during the release.
Assign: simple value assignment without changing the index count (reference counting ).
Copy: Create an object with an index count of 1 and release the old object.
Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
Use assign: for basic data types (nsinteger, cgfloat) and C data types (INT, float, double, Char, etc)
Use copy: To nsstring
Use retain: for other nsobject and its subclass
Nonatomic, non-atomic access, without synchronization, multi-thread concurrent access will improve performance. Note: If this attribute is not added, both access methods are atomic transaction access by default.
@ Property (nonatomic, retain) uitextfield * username code automatically generated during compilation
-(Uitextfield *) username {
Return username;
}
-(Void) setusername :( uitextfield *) username _{
[Username release];
Username = [username _ retain];
}
@ Property (retain) uitextfield * username automatically generated code
-(Uitextfield *) username {
Uitextfield * retval = nil;
@ Synchronized (Self ){
Retval = [[username retain] autorelease];
}
Return retval;
}
-(Void) setusername :( uitextfield *) username _{
@ Synchronized (Self ){
[Username release];
Username = [username _ retain];
}
}