浅拷贝和深拷贝
- 浅拷贝即指针拷贝,可以认为是给一只狗加了一条绳子;深拷贝不但拷贝了对象的指针,还在系统中在分配了一块内存用来存放拷贝对象的内容,可以认为是产生了一条带绳子的新狗。即浅拷贝是两根绳子一条狗,深拷贝是两根绳子两条狗且每只狗对应各自的绳子。
- 图示:
- 深浅拷贝取决于拷贝后的对象是不是和被拷贝对象的地址相同,如果不同,则产生了新的对象,即为深拷贝。如果不同,则只是指针拷贝,相当于retain一次原对象,即为浅拷贝。
copy与mutableCopy
- copy复制后的对象是不可变对象,mutableCopy复制后的对象是可变对象,与原始对象是否可变无关。
非集合类的copy与mutableCopy
- 对不可变对象执行copy操作,是指针复制(浅拷贝),执行mutableCopy操作是内容复制(深拷贝)。
- 对可变对象执行copy操作和mutableCopy操作都是内容复制(深拷贝)。
- 代码表示如下:
[immutableObject copy]; // 浅复制
[immutableObject mutableCopy]; // 深复制
[mutableObject copy]; // 深复制
[mutableObject mutableCopy] ; // 深复制
容器类的浅复制和深复制
- 容器对象和非容器对象类似,可变对象复制的都是一个新对象,不可变对象copy的是浅复制,mutableCopy是深复制
- 对于容器而言,元素对象始终是指针复制。
- 验证代码:
NSMutableArray *array01 = [NSMutableArray arrayWithObjects:@"a",@"b",@"c", nil];
NSArray *copyArray01 = [array01 copy];
NSMutableArray *mutableCopyArray01 = [array01 mutableCopy];
NSLog(@"array01 = %p,copyArray01 = %p",array01,copyArray01);
NSLog(@"array01 = %p,mutableCopyArray01 = %p",array01,mutableCopyArray01);
NSLog(@"==================================================================");
[array01 addObject:@"d"];
NSLog(@"array01 = %p,copyArray01 = %p",array01,copyArray01);
NSLog(@"array01 = %p,mutableCopyArray01 = %p",array01,mutableCopyArray01);
NSLog(@"array01 =%@\\n copyArray01 =%@\\n mutableCopyArray01 =%@",array01,copyArray01,mutableCopyArray01);
NSLog(@"array01[0] = %p,array01[1] = %p,array01[2] = %p,array01[3] = %p",array01[0],array01[1],array01[2],array01[3]);
NSLog(@"copyArray01[0] = %p,copyArray01[1] = %p,copyArray01[2] = %p",copyArray01[0],copyArray01[1],copyArray01[2]);
NSLog(@"mutableCopyArray01[0] = %p,mutableCopyArray01[1] = %p,mutableCopyArray01[2] = %p",mutableCopyArray01[0],mutableCopyArray01[1],mutableCopyArray01[2]);
NSLog(@"==================================================================");
[mutableCopyArray01 addObject:@"e"];
NSLog(@"array01 = %p,copyArray01 = %p",array01,copyArray01);
NSLog(@"array01 = %p,mutableCopyArray01 = %p",array01,mutableCopyArray01);
NSLog(@"array01 =%@\\n copyArray01 =%@\\n mutableCopyArray01 =%@",array01,copyArray01,mutableCopyArray01);
NSLog(@"array01[0] = %p,array01[1] = %p,array01[2] = %p",array01[0],array01[1],array01[2]);
NSLog(@"copyArray01[0] = %p,copyArray01[1] = %p,copyArray01[2] = %p",copyArray01[0],copyArray01[1],copyArray01[2]);
NSLog(@"mutableCopyArray01[0] = %p,mutableCopyArray01[1] = %p,mutableCopyArray01[2] = %p,mutableCopyArray01[3] = %p",mutableCopyArray01[0],mutableCopyArray01[1],mutableCopyArray01[2],mutableCopyArray01[3]);
- 打印结果:
2018-06-14 11:16:43.117758+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,copyArray01 = 0x604000443bd0
2018-06-14 11:16:43.117887+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,mutableCopyArray01 = 0x604000443a80
2018-06-14 11:16:43.118014+0800 copy_demo[1988:95724] ==================================================================
2018-06-14 11:16:43.118108+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,copyArray01 = 0x604000443bd0
2018-06-14 11:16:43.118323+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,mutableCopyArray01 = 0x604000443a80
2018-06-14 11:16:43.118485+0800 copy_demo[1988:95724] array01 =(
a,
b,
c,
d
)
copyArray01 =(
a,
b,
c
)
mutableCopyArray01 =(
a,
b,
c
)
2018-06-14 11:16:43.118559+0800 copy_demo[1988:95724] array01[0] = 0x10bf06128,array01[1] = 0x10bf06148,array01[2] = 0x10bf06168,array01[3] = 0x10bf061e8
2018-06-14 11:16:43.118661+0800 copy_demo[1988:95724] copyArray01[0] = 0x10bf06128,copyArray01[1] = 0x10bf06148,copyArray01[2] = 0x10bf06168
2018-06-14 11:16:43.118758+0800 copy_demo[1988:95724] mutableCopyArray01[0] = 0x10bf06128,mutableCopyArray01[1] = 0x10bf06148,mutableCopyArray01[2] = 0x10bf06168
2018-06-14 11:16:43.118891+0800 copy_demo[1988:95724] ==================================================================
2018-06-14 11:16:43.119034+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,copyArray01 = 0x604000443bd0
2018-06-14 11:16:43.119095+0800 copy_demo[1988:95724] array01 = 0x604000443cf0,mutableCopyArray01 = 0x604000443a80
2018-06-14 11:16:43.119185+0800 copy_demo[1988:95724] array01 =(
a,
b,
c,
d
)
copyArray01 =(
a,
b,
c
)
mutableCopyArray01 =(
a,
b,
c,
e
)
2018-06-14 11:16:43.119346+0800 copy_demo[1988:95724] array01[0] = 0x10bf06128,array01[1] = 0x10bf06148,array01[2] = 0x10bf06168
2018-06-14 11:16:43.119527+0800 copy_demo[1988:95724] copyArray01[0] = 0x10bf06128,copyArray01[1] = 0x10bf06148,copyArray01[2] = 0x10bf06168
2018-06-14 11:16:43.119595+0800 copy_demo[1988:95724] mutableCopyArray01[0] = 0x10bf06128,mutableCopyArray01[1] = 0x10bf06148,mutableCopyArray01[2] = 0x10bf06168,mutableCopyArray01[3] = 0x10bf06288
深拷贝浅拷贝面试中也经常问到,这只是一个简单的理解和总结,除了这些,还有很多比较复杂的以及自定义对象的深复制和浅复制。
推荐一篇讲得比较详细和深刻的文章:https://github.com/pro648/tips/wiki/深复制、浅复制、copy、mutableCopy。我在整理这篇文章的时候也参考了这篇文章,感谢作者的分享。