The difference between retain and copy in iOS

Source: Internet
Author: User

1, readability: readonly, ReadWrite
@property (ReadWrite,....) valueType value;
This property is the default property of the variable, that is, if you (ReadWrite and readonly are not used, then your variable is the ReadWrite property), by adding the ReadWrite property your variable will have a Get method, and a set method.
Property (readonly,...) ValueType value;
This property variable means that the variable is only readable, that is, you can only use its Get method.
2, assign, setter method directly assigned value, do not perform any retain operation, in order to solve the original type and circular reference problem
3, retain, setter method to release the old value of the parameter and then retain the new value, all implementations are in this order
4, copy, setter method copy operation, and retain processing process, the first old value release, and then copy the new object, Retaincount 1. This is the mechanism introduced to reduce dependency on the context.
5, 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 lock is added to the owning object instance level. So it's safe not to add nonatomic to multithreading.
Attached to the net text:
Retain vs. Copy
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
What the hell does that mean?
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.
Note: The NSString class example is wrong. NSString does not have retain, and statically allocates a memory when defined.

The difference between retain and copy in iOS

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.