一、概念
1莫秆、淺拷貝是對(duì)內(nèi)存地址的復(fù)制条霜,目標(biāo)對(duì)象指針和原對(duì)象指向同一塊內(nèi)存空間催什,淺拷貝會(huì)使原對(duì)象的引用計(jì)數(shù)+1,即新創(chuàng)建了一個(gè)指向原對(duì)象的新指針而已宰睡,并沒有創(chuàng)建一個(gè)全新的對(duì)象蒲凶,當(dāng)內(nèi)存銷毀的時(shí)候气筋,指向這片內(nèi)存的指針需要重新定義才可以使用,不然會(huì)成為野指針旋圆。
2裆悄、深拷貝是對(duì)原對(duì)象具體內(nèi)容的復(fù)制,而內(nèi)存地址是自主分配的臂聋,拷貝之后光稼,兩個(gè)對(duì)象雖然存儲(chǔ)的內(nèi)容是相同 ,但是內(nèi)存地址不一樣孩等,兩個(gè)對(duì)象是相互獨(dú)立的艾君,互不干涉。
### 3肄方、總結(jié):
深拷貝就是內(nèi)容拷貝冰垄,淺拷貝就是指針拷貝。
區(qū)別在于是否開啟新的內(nèi)存地址权她,是否影響內(nèi)存地址的引用計(jì)數(shù)
二虹茶、示例分析
1、非集合對(duì)象的copy與mutableCopy
1.1 不可變對(duì)象NSString
NSString 的copy是淺拷貝隅要,且copy返回的對(duì)象是不可變對(duì)象蝴罪,mutableCopy是深拷貝。
1.2 可變對(duì)象NSMutableString
NSMutableString的copy和mutableCopy都是深拷貝步清,copy返回的對(duì)象是不可變對(duì)象
2要门、集合對(duì)象的copy與mutableCopy
2.1 不可變對(duì)象NSArray
copy是淺拷貝,mutableCopy是深拷貝廓啊,且copy返回的對(duì)象是不可變對(duì)象
2.2 可變對(duì)象NSMutableArray
NSMutableArray的copy和mutableCopy都是深拷貝欢搜,copy返回的對(duì)象是不可變對(duì)象
特別注意的是:對(duì)于集合類的可變對(duì)象來說,深拷貝并非嚴(yán)格意義上的深復(fù)制谴轮,只能算是單層深復(fù)制炒瘟,即雖然新開辟了內(nèi)存地址,但是存放在內(nèi)存上的值(也就是數(shù)組里的元素仍然之鄉(xiāng)員數(shù)組元素值第步,并沒有另外復(fù)制一份)疮装,這就叫做單層深復(fù)制。
舉例說明:
- (void)singleNSMutableArrayTest
{
NSMutableArray *marry1 = [[NSMutableArray alloc] init];
NSMutableString *mstr1 = [[NSMutableString alloc]initWithString:@"value1"];
NSMutableString *mstr2 = [[NSMutableString alloc]initWithString:@"value2"];
[marry1 addObject:mstr1];
[marry1 addObject:mstr2];
NSMutableArray *marry2 = [marry1 copy];
NSMutableArray *marry3 = [marry1 mutableCopy];
NSLog(@"marry1:%p - %@ ",marry1,marry1);
NSLog(@"marry2:%p - %@ ",marry2,marry2);
NSLog(@"marry3:%p - %@ ",marry3,marry3);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry1[0],marry1[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry2[0],marry2[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry3[0],marry3[1]);
NSLog(@" ------------------修改原值后------------------------ ");
[mstr1 appendFormat:@"aaa"];
NSLog(@"marry1:%p - %@ ",marry1,marry1);
NSLog(@"marry2:%p - %@ ",marry2,marry2);
NSLog(@"marry3:%p - %@ ",marry3,marry3);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry1[0],marry1[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry2[0],marry2[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry3[0],marry3[1]);
}
打印結(jié)果:
2017-07-20 19:48:24.539 beck.wang[1750:230132] marry1:0x60800004ae00 - (
value1,
value2
)
2017-07-20 19:48:24.539 beck.wang[1750:230132] marry2:0x608000023f00 - (
value1,
value2
)
2017-07-20 19:48:24.539 beck.wang[1750:230132] marry3:0x60800004abc0 - (
value1,
value2
)
2017-07-20 19:48:24.540 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
2017-07-20 19:48:24.540 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
2017-07-20 19:48:24.540 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
2017-07-20 19:48:24.540 beck.wang[1750:230132]
------------------修改原值后------------------------
2017-07-20 19:48:24.540 beck.wang[1750:230132] marry1:0x60800004ae00 - (
value1aaa,
value2
)
2017-07-20 19:48:24.540 beck.wang[1750:230132] marry2:0x608000023f00 - (
value1aaa,
value2
)
2017-07-20 19:48:24.540 beck.wang[1750:230132] marry3:0x60800004abc0 - (
value1aaa,
value2
)
2017-07-20 19:48:24.541 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
2017-07-20 19:48:24.541 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
2017-07-20 19:48:24.541 beck.wang[1750:230132] 數(shù)組元素地址:value1:0x60800006df40 - value2:0x60800006cb40
分析:在修改原值之前雌续,marry1斩个、marry2、marr3 地址都不一樣驯杜,很明顯copy和mutableCopy都是深拷貝受啥,但是從修改原值后的打印結(jié)果來看,這里的深拷貝只是單層深拷貝:新開辟了內(nèi)存地址,但是數(shù)組中的值還是指向原數(shù)組的滚局,這樣才能在修改原值后居暖,marry2 marr3中的值都修改了。另外藤肢,從打印的數(shù)組元素地址可以很明顯的看出來太闺,修改前后marry1、marry嘁圈、marr3的數(shù)組元素地址都是一模一樣的省骂,更加佐證了這一點(diǎn)。
2.3 思維擴(kuò)展:集合對(duì)象的完全深拷貝
2.2中提到了集合類的對(duì)象來說最住,深拷貝只是單層深拷貝钞澳,那有沒有辦法實(shí)現(xiàn)每一層都深拷貝呢?回答是肯定的涨缚,目前我們可以這么做:
(1)歸檔解檔大法
- (void) deplyFullCopy
{
NSMutableArray *marry1 = [[NSMutableArray alloc] init];
NSMutableString *mstr1 = [[NSMutableString alloc]initWithString:@"value1"];
NSMutableString *mstr2 = [[NSMutableString alloc]initWithString:@"value2"];
[marry1 addObject:mstr1];
[marry1 addObject:mstr2];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:marry1];
NSArray *marray2 = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:data error:nil];
NSLog(@"marry1:%p - %@ ",marry1,marry1);
NSLog(@"marry2:%p - %@ ",marray2,marray2);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry1[0],marry1[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marray2[0],marray2[1]);
}
打印結(jié)果:
2017-07-20 20:04:38.726 beck.wang[1833:242158] marry1:0x600000048a00 - (
value1,
value2
)
2017-07-20 20:04:38.726 beck.wang[1833:242158] marry2:0x600000049780 - (
value1,
value2
)
2017-07-20 20:04:38.726 beck.wang[1833:242158] 數(shù)組元素地址:value1:0x600000066300 - value2:0x600000067000
2017-07-20 20:04:38.726 beck.wang[1833:242158] 數(shù)組元素地址:value1:0x600000066740 - value2:0x600000066f40
分析:我們可以看到轧粟,開辟了新的內(nèi)存地址的同時(shí),數(shù)組元素的指針地址也不同了脓魏,實(shí)現(xiàn)了完全的深拷貝兰吟。
(2)
- (void) deplyFullCopy2
{
NSMutableArray *marry1 = [[NSMutableArray alloc] init];
NSMutableString *mstr1 = [[NSMutableString alloc]initWithString:@"value1"];
NSMutableString *mstr2 = [[NSMutableString alloc]initWithString:@"value2"];
[marry1 addObject:mstr1];
[marry1 addObject:mstr2];
NSArray *marray2 = [[NSArray alloc] initWithArray:marry1 copyItems:YES];
NSLog(@"marry1:%p - %@ ",marry1,marry1);
NSLog(@"marry2:%p - %@ ",marray2,marray2);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marry1[0],marry1[1]);
NSLog(@"數(shù)組元素地址:value1:%p - value2:%p ",marray2[0],marray2[1]);
}
打印結(jié)果:
2017-07-20 20:08:04.201 beck.wang[1868:246161] marry1:0x610000050320 - (
value1,
value2
)
2017-07-20 20:08:04.202 beck.wang[1868:246161] marry2:0x6100002214c0 - (
value1,
value2
)
2017-07-20 20:08:04.202 beck.wang[1868:246161] 數(shù)組元素地址:value1:0x610000265600 - value2:0x610000266400
2017-07-20 20:08:04.202 beck.wang[1868:246161] 數(shù)組元素地址:value1:0xa003165756c61766 - value2:0xa003265756c61766
分析:同上。
三茂翔、準(zhǔn)則
1混蔼、可變對(duì)象的copy和mutableCopy方法都是深拷貝(區(qū)別完全深拷貝和單層深拷貝)
2、不可變對(duì)象的copy方法是淺拷貝檩电,mutableCopy是深拷貝
3拄丰、copy方法返回的對(duì)象都是不可變對(duì)象
參考鏈接