Copy and Mutablecopy notes

Source: Internet
Author: User

Gao Li 228 | Source: Blog Park | 2012/4/22 14:53:20 | Read 4 times

NSString *a = [[NSString alloc]initwithstring:@ "Hello"];
NSString *b = [a copy];
NSLog (@ "%d%d", A.retaincount, B.retaincount);//2,2

Nsmutablestring *f = [a mutablecopy];

NSLog (@ "%d%d", A.retaincount, F.retaincount);//2,1

[F appendstring:@ "DD"];
NSLog (@ "%@,%@", a,f);//Hello,hellodd
//For an immutable object, copy is a shallow copy, but the pointer copies its Retaincount 1
//Mutablecopy is a deep copy is an object copy
    
nsmutablestring *c = [[nsmutablestring alloc]initwithstring:@ "Hello"];
Nsmutablestring *d = [C copy];
NSLog (@ "%d%d", c.retaincount,d.retaincount);//
[D appendstring:@ "ddd"]; Error because copy returns a non-changing object
Nsmutablestring *e = [C mutablecopy];
NSLog (@ "%d%d", c.retaincount,e.retaincount);//
[E appendstring:@ "dddd"];
NSLog (@ "%@,%@", c,e);//HELLO,HELLODDDD
//For mutable objects copy and mutablecopy are both deep copy and copy objects but the object returned by copy is an immutable object
//------------------------------------------------------------//
For System container class objects

Nsarray *array1 = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil];
Nsarray *array1copy = [array1 copy];
NSLog (@ "%d,%d", array1.retaincount,array1copy.retaincount);//2,2
Nsmutablearray *arraymcopy = [Array1 mutablecopy];
NSLog (@ "%d,%d", array1.retaincount,arraymcopy.retaincount);//2,1
[Arraymcopy removeobjectatindex:0];
[Arraymcopy addobject:@ "D"];
NSLog (@ "%@-----%@", array1,arraymcopy);//a,b,c-----b,c,d
Nsarray *marray1 = [Nsarray arraywithobjects:[nsmutablestring stringwithstring:@ "A"],@ "B", @ "C", nil];
Nsarray *marray1copy = [mArray1 copy];
NSLog (@ "%d,%d", marray1.retaincount,marray1copy.retaincount);//2,2
Nsmutablearray *marraymcopy = [MArray1 mutablecopy];
NSLog (@ "%d,%d", marray1.retaincount,marraymcopy.retaincount);//2,1
nsmutablestring *temp = [MArray1 objectatindex:0];
NSLog (@ "%@", temp);//a
[Temp appendstring:@ "AA"];
NSLog (@ "%@", mArray1);//AAA b C
NSLog (@ "%@", marray1copy); AAA b C
NSLog (@ "%@", marraymcopy);//AAA b C
//For a container, its element object is always a pointer copy. If you need an element object that is also an object copy, you need to implement a deep copy.
nsarray* Truedeepcopyarray = [Nskeyedunarchiver unarchiveobjectwithdata:
[Nskeyedarchiver Archiveddatawithrootobject:marray1]];
nsmutablestring *TEMP2 = [Truedeepcopyarray objectatindex:0];
[Temp2 appendstring:@ "AA"];

NSLog (@ "%@", mArray1);//AAA b C
NSLog (@ "%@", Truedeepcopyarray);//AAAAA b C
Truedeepcopyarray is a deep copy in full sense.

Copy and Mutablecopy notes

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.