Assign: Simple assignment, not changing index count
Copy: Create an object with an index count of 1 and then release the old object
Retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1
Copy actually creates an identical object, and retain is not:
such as a NSString object, the address is 0x1111, the content is @ "STR"
Copy to another nsstring, the address is 0x2222, the content is the same, the new object retain is 1, the old object has not changed
Retain to another nsstring, the same address (set up a pointer, pointer copy), the content of course the same, the object's retain value +1
That is, retain is a pointer copy and copy is a copy of the content. The old object will be released before the copy.
* Using assign: for underlying data types (Nsinteger) and C data types (int, float, double, char, etc.)
* Use copy: for NSString
* Use retain: For other nsobject and its subclasses
1.readonly indicates that this property is read-only, that is, only the getter method is generated, and the setter method is not generated.
2.readwrite, setting the available access level
3.retain, is to indicate that the property at the time of assignment, first release before the value, and then assign a new value to the property, the reference plus 1.
4.nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access improves performance. Note that if you do not add this property, the default is two access methods are atomic transaction access.
The difference between retain and copy and assign.
1. Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a value to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.
2. Understand the problem of assign in 1, then how to solve? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.
3. Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and 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 multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:
if (Property! = newvalue) {
[Property release];
property = [NewValue retain];
}
About retain,copy,assign The difference problem actually troubled me for a long time, because in the program is not too often used to copy,assign, so the specific differences of the three have not quite understood.
According to my understanding, assign and retain difference, is introduced a counter retaincount, can be a memory of the release of a lot of convenience. Copy, is to copy the original memory again, so that each has a memory, so that the release of the time will not be wrong.
Assign: Simple assignment, without changing the index count (Reference counting).
Copy: Create an object with an index count of 1 and then release the old object
Retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1
Use assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)
Using copy: On NSString
Use retain: For other nsobject and its subclasses
Nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access improves performance. Note that if you do not add this property, the default is two access methods are atomic transaction access
@property (nonatomic, retain) Uitextfield *username automatically generated code at compile time
-(Uitextfield *) UserName {
return userName;
}
-(void) Setusername: (Uitextfield *) username_ {
[UserName release];
UserName = [username_ retain];
}
@property (retain) Uitextfield *username Auto-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];
}
}
Meaning of assign, copy, retain and other keywords in ios