Change in reference count before and after copy modification for NSString

Source: Internet
Author: User

1NSLog (@"NSString object, use NSString to pick up the---------------------------------");2NSString *string1 = [[NSString Alloc]initwithformat:@"Immutable String"];3NSLog (@"reference count of immutable strings before copy =%ld", [string1 retaincount]);4         5NSString *copystring1 =string1.copy;6NSLog (@"reference count of immutable strings after copy =%ld", [string1 retaincount]);7NSLog (@"after copy, the reference count of copystring =%ld", [copyString1 Retaincount]);8         9NSLog (@"%p", string1);TenNSLog (@"%p", copyString1); OneNSLog (@"nsmutablestring object, use nsmutablestring to pick up the---------------------------------"); Ansmutablestring *string2 = [[nsmutablestring Alloc]initwithformat:@"variable String"]; -NSLog (@"reference count of mutable strings before copy =%ld", [string2 Retaincount]); -          theNsmutablestring *copystring2 =string2.copy; -NSLog (@"reference count of mutable strings after copy =%ld", [string2 Retaincount]); -NSLog (@"after copy, the reference count of copystring =%ld", [copyString2 Retaincount]); -          +NSLog (@"%p", string2); -NSLog (@"%p", copyString2); +NSLog (@"NSString object, use nsmutablestring to pick up the---------------------------------"); ANSString *string3 = [[NSString Alloc]initwithformat:@"Immutable String"]; atNSLog (@"reference count of immutable strings before copy =%ld", [String3 Retaincount]); -          -Nsmutablestring *copystring3 =string3.copy; -NSLog (@"reference count of immutable strings after copy =%ld", [String3 Retaincount]); -NSLog (@"after copy, the reference count of copystring =%ld", [CopyString3 Retaincount]); -          inNSLog (@"%p", string3); -NSLog (@"%p", copyString3); toNSLog (@"nsmutablestring object, use NSString to pick up the---------------------------------"); +nsmutablestring *string4 = [[nsmutablestring Alloc]initwithformat:@"variable String"]; -NSLog (@"reference count of mutable strings before copy =%ld", [String4 Retaincount]); the          *NSString *copystring4 =string4.copy; $NSLog (@"reference count of mutable strings after copy =%ld", [String4 Retaincount]);Panax NotoginsengNSLog (@"after copy, the reference count of copystring =%ld", [CopyString4 Retaincount]); -          theNSLog (@"%p", String4); +NSLog (@"%p", CopyString4);

Let's take a look at the running results above and print out what: 1 -- all:06.411 oclession9[1810:63941] NSString object, use NSString to pick up the---------------------------------

2  .- .- the  One: the:06.411oclession9[1810:63941] before copy, the reference count of immutable strings =13  .- .- the  One: the:06.411oclession9[1810:63941] After copy, the reference count of the immutable string =24  .- .- the  One: the:06.411oclession9[1810:63941] After copy, the reference count for copystring =25  .- .- the  One: the:06.411oclession9[1810:63941]0x1002027406  .- .- the  One: the:06.411oclession9[1810:63941]0x1002027407  .- .- the  One: the:06.412oclession9[1810:63941] Nsmutablestring object, use nsmutablestring to pick up the---------------------------------8  .- .- the  One: the:06.412oclession9[1810:63941] before copy, the reference count of a mutable string =19  .- .- the  One: the:06.412oclession9[1810:63941] After copy, the reference count of the mutable string =1Ten  .- .- the  One: the:06.412oclession9[1810:63941] After copy, the reference count for copystring =1 One  .- .- the  One: the:06.412oclession9[1810:63941]0x100300840 A  .- .- the  One: the:06.412oclession9[1810:63941]0x100300810 -  .- .- the  One: the:06.412oclession9[1810:63941] NSString object, use nsmutablestring to pick up the--------------------------------- -  .- .- the  One: the:06.412oclession9[1810:63941] before copy, the reference count of immutable strings =1 the  .- .- the  One: the:06.412oclession9[1810:63941] After copy, the reference count of the immutable string =2 -  .- .- the  One: the:06.412oclession9[1810:63941] After copy, the reference count for copystring =2 -  .- .- the  One: the:06.412oclession9[1810:63941]0x100106f90 -  .- .- the  One: the:06.412oclession9[1810:63941]0x100106f90 +  .- .- the  One: the:06.413oclession9[1810:63941] Nsmutablestring object, use NSString to pick up the--------------------------------- -  .- .- the  One: the:06.413oclession9[1810:63941] before copy, the reference count of a mutable string =1 +  .- .- the  One: the:06.413oclession9[1810:63941] After copy, the reference count of the mutable string =1 A  .- .- the  One: the:06.413oclession9[1810:63941] After copy, the reference count for copystring =1 at  .- .- the  One: the:06.413oclession9[1810:63941]0x1006002f0
2016-06-15 11:39:06.413 oclession9[1810:63941] 0x100600210

Conclusion: For a string, only the immutable copy of the immutable string is a shallow copy, and the rest is a deep copy.

Change in reference count before and after copy modification for NSString

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.