Meanings of keywords such as assign, copy, and retain in iOS

Source: Internet
Author: User

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];
}
}

 

 

From Qingfeng Column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.