OC syntax--Ask the specific difference between copy and retain in the NSString attribute declaration

Source: Internet
Author: User

Q nsstring The specific differences between copy and retain in the attribute declarationcopy is the source object release and then the new object copy is paid to the source object
retain is to release the source objectafter the new object retain again to the source object
The fundamental difference is that when the object makes a deep copy, copy wants to open up new storage space, and the effect is the same when the shallow copy. The reference count facet retain is the source object reference count plus 1,copy when
is shallow copy when the source object reference count plus 1, deep copy when the source object reference count is not changed, new object reference count plus 1.

nsstring *str = @ "Hello";

Copy of copy original mode

nsmutablestring *STRM = [str copy];

[StrM appendstring:@ "123"]; it is not working because copy is copied just as it is, and the memory address is the same. Can't think of a different type pointing at him, he's this thing.

Deep copy: To change

Shallow copy: Unchanged

OC syntax--Ask the specific difference between copy and retain in the NSString attribute declaration

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.