The difference between a deep copy and a shallow copy of iOS and copy and strong

Source: Internet
Author: User

It was all about remembering to keep copy and strong and asking me. I don't know what I'm talking about.

Verify with simple code. Sometimes it takes a little time to verify that you can make your ideas clearer.

nsmutablestring * mustr = [nsmutablestring stringwithstring:@ "good"];

Self . str = mustr;

Self . fuzhistring = mustr;

NSLog(@ "Self.str is (%@) = = = Str,self.fuzhistring is (%@)", self. Str,self. Fuzhistring);

NSLog(@ "Self.str *p is (%@) = = = Str,self.fuzhistring *p is (%@)", self. Str,self. Fuzhistring);

Note: Print results = = Self.str is (good) = = Str,self.fuzhistring is (good)

Self.str *p is (0x600000059350) = Str,self.fuzhistring *p is (0xa000000646f6f674)

[Mustr appendString:@ "Night"];

NSLog(@ "Self.str is (%@) = = = Str,self.fuzhistring is (%@)", self. Str,self. Fuzhistring);

NSLog(@ "Self.str *p is (%@) = = = Str,self.fuzhistring *p is (%@)", self. Str,self. Fuzhistring);

Note: printing results ==self.str is (goodnight) = Str,self.fuzhistring is (good)

Self.str *p is (0x600000059350) = Str,self.fuzhistring *p is (0xa000000646f6f674)

Print Result: Copy Modifier's property is deep copy (opens a new memory to point to a new value)

The properties of the strong modifier are light copy (which changes with the value of MUSTR);

Also indicates that the string in order to prevent inadvertent modification should use copy, develop good habits.

Of course, there's no difference between using copy and strong with NSString.

The difference between a deep copy and a shallow copy of iOS and copy and strong

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.