What is the difference between Retain and Copy in IOS? iosretaincopy

Source: Internet
Author: User

What is the difference between Retain and Copy in IOS? iosretaincopy

1. Readability: readonly and readwrite
@ Property (readwrite,...) valueType value;
This attribute is the default attribute of the variable. If you (readwrite and readonly are not used, your variable is the readwrite attribute), your variable will have a get method by adding the readwrite attribute, and set method.
Property (readonly,...) valueType value;
This attribute variable indicates that the variable has only the readable method, that is, you can only use its get method.
2. The assign and setter Methods assign values directly without any retain operations. To solve the problem of the original type and cycle reference
3. The retain and setter Methods release the old values of parameters and retain the new values. All implementations are in this order.
4. copy and setter Methods perform the Copy operation. Like the retain process, the old value is release first, and then the new object is copied. retainCount is 1. This is a mechanism introduced to reduce context dependencies.
5. nonatomic: Non-atomic access without synchronization. multi-thread concurrent access improves performance.
Note: If this attribute is not added, both access methods are atomic transaction access by default. The lock is added to the object instance level. Therefore, it is safe to use nonatomic without multithreading.
Appendix:
Retain vs. Copy
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.
What is the above damn meaning?
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 reaches 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.
Note: The NSString class is incorrect. If NSString does not have a retain, a memory segment is allocated statically.

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.