理解copy与mutableCopy

个人理解,如有不正确的地方望斧正。

顾名思义,mutable:可变的。如:NSMutableArray,可变数组;NSMutableDictionary,可变字典;NSMutableString,可变字符串等等。

mutableCopy,就是copy一个mutable的对象,如上。也就是所谓的深拷贝。
copy,就是copy一个NSArray,NSDictionary,NSString等等这类的对象。就是所谓的浅拷贝。

网上查阅资料发现,浅拷贝,copy的是指针,并没有copy里面的内容。深拷贝,就是把里面的内容也给拷贝了。

基于以上理解,我写了如下Demo:

NSString *string1 = @"YES";
NSString *string2 = [string1 copy];
NSLog(@"string1 is %@",string1);
NSLog(@"string2 is %@",string2);
string1 = @"NO";
NSLog(@"string1 is %@",string1);
NSLog(@"string2 is %@",string2);

因为拷贝的只是指针,所以当指针指向地址的内容发生变化时,string1和string2都会发生变化。Run it

string1 is YES 
string2 is YES 
string1 is NO
string2 is YES 

脸疼。Why?进一步详细输出日志

NSString *string1 = @"YES";
NSString *string2 = [string1 copy];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
string1 = @"No";
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);

Run

string1 is YES address is 0x10f6d41a0
string2 is YES address is 0x10f6d41a0
string1 is No address is 0x10f6d4200
string2 is YES address is 0x10f6d41a0

可以看到string1的指针地址确实复制给string2了,但之所以string2没有跟着string1变化而变化,是因为在string1 = @"No";时,string1指向了其他的地址,而string2还在之前的地址。所以string1变成了新的,而string2不变。
因为NSString对象是不可变的,你不能直接改变他的内容,只能重新给他赋值,而赋值,就会指向新的地址。
如果我们采用NSMutableString

NSMutableString *string1 = [[NSMutableString alloc]initWithString:@"YES"];
NSString *string2 = [string1 copy];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
[string1 appendString:@" or Not"];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
string1 is YES address is 0x608000267400
string2 is YES address is 0xa000000005345593
string1 is YES or Not address is 0x608000267400
string2 is YES address is 0xa000000005345593

copy只能复制不可变的对象,所以对可变的NSMutableString进行copy时,已经生成了新的对象。
做到这儿了再来试试mutableCopy

NSMutableString *string1 = [[NSMutableString alloc]initWithString:@"YES"];
NSMutableString *string2 = [string1 mutableCopy];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
[string1 appendString:@" or Not"];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
string1 is YES address is 0x618000072240
string2 is YES address is 0x618000072140
string1 is YES or Not address is 0x618000072240
string2 is YES address is 0x618000072140

由此推论,所谓的深拷贝,只拷贝了内容,并没有拷贝指针。
如果我们想要既拷贝内容,又拷贝指针咋办?什么都一样其实就是同一个对象了。

NSMutableString *string1 = [[NSMutableString alloc]initWithString:@"YES"];
NSMutableString *string2 = string1;
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
[string1 appendString:@" or Not"];
NSLog(@"string1 is %@ address is %p",string1, string1);
NSLog(@"string2 is %@ address is %p",string2, string2);
string1 is YES address is 0x61800006d5c0
string2 is YES address is 0x61800006d5c0
string1 is YES or Not address is 0x61800006d5c0
string2 is YES or Not address is 0x61800006d5c0

综上,
1、copy针对不可变对象,只拷贝指针。(但因为不可变,所以内容也就一样不会变。)如NSString,string2 = [string1 copy];等效于string2 = string1
2、mutableCopy针对可变对象,只拷贝内容,会分配新的指针地址。所以如NSMutableString,string2 = [string1 mutableCopy];不等效string2 = string1
3、可变对象进行copy的话,指针地址会发生改变。不可变对象进行mutableCopy的话,指针地址也会发生改变。


以上是对copy与mutableCopy的理解,顺便提一下,不是所以的对象都可以copy与mutableCopy的,用户自定义的NSObject需要实现NSCopying 协议,然后自己实现copyWithZone或者mutableCopyWithZone方法。
(关于property中的copy在这一篇)


最后警醒一下自己:

NSString *string = @"test";
NSString *copy1 = [string copy];
NSString *copy2 = [string mutableCopy];
NSMutableString *copy3 = [string copy];
NSMutableString *copy4 = [string mutableCopy];
NSLog(@"string is %@ address is %p &address %p",string, string, &string);
NSLog(@" copy1 is %@ address is %p &address %p",copy1, copy1, ©1);
NSLog(@" copy2 is %@ address is %p &address %p",copy2, copy2, ©2);
NSLog(@" copy3 is %@ address is %p &address %p",copy3, copy3, ©3);
NSLog(@" copy4 is %@ address is %p &address %p",copy4, copy4, ©4);
string is test address is 0x1092191a0 &address 0x7fff56a22a38
 copy1 is test address is 0x1092191a0 &address 0x7fff56a22a30
 copy2 is test address is 0x60000026ea00 &address 0x7fff56a22a28
 copy3 is test address is 0x1092191a0 &address 0x7fff56a22a20
 copy4 is test address is 0x60000026efc0 &address 0x7fff56a22a18

之前陷入误区,&string去取指针地址,发现每一次的地址都不相同。其实我们的string本身就是指针变量,%p即是指针地址,如果再加上&,取的是指针的指针的地址。
同志仍需努力啊!

你可能感兴趣的:(理解copy与mutableCopy)