标签:image 复杂 ios 元素对象 nil 系统 inf src obj
深浅拷贝取决于拷贝后的对象是不是和被拷贝对象的地址相同,如果不同,则产生了新的对象,即为深拷贝。如果不同,则只是指针拷贝,相当于retain一次原对象,即为浅拷贝。
copy复制后的对象是不可变对象,mutableCopy复制后的对象是可变对象,与原始对象是否可变无关。
代码表示如下:
[immutableObject copy]; // 浅复制
[immutableObject mutableCopy]; // 深复制
[mutableObject copy]; // 深复制
[mutableObject 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/%E6%B7%B1%E5%A4%8D%E5%88%B6%E3%80%81%E6%B5%85%E5%A4%8D%E5%88%B6%E3%80%81copy%E3%80%81mutableCopy。我在整理这篇文章的时候也参考了这篇文章,感谢作者的分享。
标签:image 复杂 ios 元素对象 nil 系统 inf src obj
原文地址:https://www.cnblogs.com/zhanqin/p/9182235.html