深拷贝和完全拷贝对比的探究
上篇文章assign/retain/copy及深淺拷貝的區別寫到淺拷貝,深拷貝,以及完全拷貝,包括由整理的 demo得出的一些結論。 今天就來就深拷貝和完全拷貝來做一些探究。
注意
這里集合類中的對象使用的是儲存在堆區的對象,不可以用常量區的對象,否則會影響最終結果。
深拷貝數組
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSArray *array = [NSArray arrayWithObjects:elementOne, nil];NSMutableArray *arr_mc = [array mutableCopy];NSLog(@"array: 第一層 = %p, 第二層element = %p",array,array[0]); NSLog(@"arr_mc: 第一層 = %p, 第二層element = %p",arr_mc,arr_mc[0]);深拷貝打印結果
array: 第一層 = 0x7ff50bf16b60, 第二層element = 0x7ff50bf16b30 arr_mc: 第一層 = 0x7ff50bf51270, 第二層element = 0x7ff50bf16b30深拷貝總結
根據打印結果可以發現,深拷貝上面的數組時,其中數組arr_mc 會另外開辟一個空間來存儲,但是其中的元素的地址卻沒有另外開辟一個空間來存儲。 這個理解是不對的,其實深拷貝時數組arr_mc 和其中的元素的地址確實改變了,不要忘了數組arr_mc的地址就等于它首元素的地址(%p打印&array[0])。
但是,這個元素中存儲的是一個指針,而且這個指針就是數組array中的elementOne的指針。 這就說明當進行 mutableCopy 時,會將整個集合拷貝到一個新開辟的內存空間,但是里面的內容或者說指針并沒有改變,而是一并拷貝過來。
那么如何做到全部都拷貝過來呢?這就是完全拷貝了。
完全拷貝
這里要說的完全拷貝,顧名思義,就是利用遞歸的原理,將集合以及集合中所有層次的集合和對象進行 mutableCopy,達到完全復制的效果。
自定義完全拷貝
利用遞歸的原理,將集合以及集合中所有層次的集合和對象進行 mutableCopy,達到完全復制的效果。
//NSDictionary (DCCompleteCopy) - (NSMutableArray *)mutableDeepCopy {NSUInteger count = [self count];id cArray[count];for (NSUInteger i = 0; i < count; i++) {id obj = self[i];if ([obj respondsToSelector:@selector(mutableDeepCopy)]) {cArray[i] = [obj mutableDeepCopy];} else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) {cArray[i] = [obj mutableCopy];} else if ([obj respondsToSelector:@selector(deepCopy)]) {cArray[i] = [obj deepCopy];} else if ([obj respondsToSelector:@selector(copyWithZone:)]) {cArray[i] = [obj copy];} else {//DLog(@"********Error:NSArray MutableDeepCopy Failed!!! ********");return nil;}}return [NSMutableArray arrayWithObjects:cArray count:count]; }//NSDictionary (DCCompleteCopy) - (NSMutableDictionary *)mutableDeepCopy {NSUInteger count = [self count];id cObjects[count];id cKeys[count];NSEnumerator *e = [self keyEnumerator];NSUInteger i = 0;id thisKey;while ((thisKey = [e nextObject]) != nil) {id obj = self[thisKey];if ([obj respondsToSelector:@selector(mutableDeepCopy)]) {cObjects[i] = [obj mutableDeepCopy];} else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) {cObjects[i] = [obj mutableCopy];} else if ([obj respondsToSelector:@selector(deepCopy)]) {cObjects[i] = [obj deepCopy];} else if ([thisKey respondsToSelector:@selector(copyWithZone:)]) {cObjects[i] = [thisKey copy];} else {//DLog(@"********Error:NSDictionary Key DeepCopy Failed!!! ********")return nil;}if ([thisKey respondsToSelector:@selector(deepCopy)]) {cKeys[i] = [thisKey deepCopy];} else if ([thisKey respondsToSelector:@selector(copyWithZone:)]) {cKeys[i] = [thisKey copy];} else {//DLog(@"********Error:NSDictionary Key DeepCopy Failed!!! ********")return nil;}++i;}return [NSMutableDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count]; }數組兩層完全拷貝
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSArray *array = [NSArray arrayWithObjects:elementOne, nil];NSMutableArray *arr_mc = [array mutableCopy]; NSMutableArray *arr_mdc = [array mutableDeepCopy];NSLog(@"array: 第一層 = %p, 第二層element = %p",array,array[0]); NSLog(@"arr_mc: 第一層 = %p, 第二層element = %p",arr_mc,arr_mc[0]); NSLog(@"arr_mdc:第一層 = %p, 第二層element = %p",arr_mdc,arr_mdc[0]);打印結果:
array: 第一層 = 0x7fe488e12f90, 第二層element = 0x7fe488e16910 arr_mc: 第一層 = 0x7fe488e0a6b0, 第二層element = 0x7fe488e16910 arr_mdc:第一層 = 0x7fe488e0f880, 第二層element = 0x7fe488e12f50數組三層完全拷貝
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSArray *arr_one = [NSArray arrayWithObjects:elementOne, nil]; NSArray *array = [NSArray arrayWithObjects:arr_one, nil];NSMutableArray *arr_mc = [array mutableCopy]; NSMutableArray *arr_mdc = [array mutableDeepCopy];NSLog(@"array: 第一層 = %p, 第二層 = %p, 第三層element = %p",array,array[0],array[0][0]); NSLog(@"arr_mc: 第一層 = %p, 第二層 = %p, 第三層element = %p",arr_mc,arr_mc[0],arr_mc[0][0]); NSLog(@"arr_mdc:第一層 = %p, 第二層 = %p, 第三層element = %p",arr_mdc,arr_mdc[0],arr_mdc[0][0]);打印結果:
>array: 第一層 = 0x7fe488e138e0, 第二層 = 0x7fe488e16df0, 第三層element = 0x7fe488e08f80 arr_mc: 第一層 = 0x7fe488e13900, 第二層 = 0x7fe488e16df0, 第三層element = 0x7fe488e08f80 arr_mdc:第一層 = 0x7fe488e16890, 第二層 = 0x7fe488e16860, 第三層element = 0x7fe488e138a0字典兩層完全拷貝
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:elementOne, @"element", nil];NSMutableDictionary *dic_mc = [dic mutableCopy]; NSMutableDictionary *dic_mdc = [dic mutableDeepCopy];NSLog(@"dic: 第一層 = %p, 第二層element = %p",dic,dic[@"element"]); NSLog(@"dic_mc: 第一層 = %p, 第二層element = %p",dic_mc,dic_mc[@"element"]); NSLog(@"dic_mdc:第一層 = %p, 第二層element = %p",dic_mdc,dic_mdc[@"element"]);打印結果:
dic: 第一層 = 0x7fe488c0a070, 第二層element = 0x7fe488c0d3c0 dic_mc: 第一層 = 0x7fe488c05370, 第二層element = 0x7fe488c0d3c0 dic_mdc:第一層 = 0x7fe488c24a10, 第二層element = 0x7fe488c1ed70字典三層完全拷貝
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSDictionary *dic_one = [NSDictionary dictionaryWithObjectsAndKeys:elementOne, @"element", nil]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:dic_one, @"one", nil];NSMutableDictionary *dic_mc = [dic mutableCopy]; NSMutableDictionary *dic_mdc = [dic mutableDeepCopy];NSLog(@"dic: 第一層 = %p, 第二層 = %p, 第三層element = %p",dic,dic[@"one"],dic[@"one"][@"element"]); NSLog(@"dic_mc: 第一層 = %p, 第二層 = %p, 第三層element = %p",dic_mc,dic_mc[@"one"],dic_mc[@"one"][@"element"]); NSLog(@"dic_mdc:第一層 = %p, 第二層 = %p, 第三層element = %p",dic_mdc,dic_mdc[@"one"],dic_mdc[@"one"][@"element"]);打印結果:
dic: 第一層 = 0x7fe488e14c60, 第二層 = 0x7fe488e168c0, 第三層element = 0x7fe488e14c30 dic_mc: 第一層 = 0x7fe488e14ca0, 第二層 = 0x7fe488e168c0, 第三層element = 0x7fe488e14c30 dic_mdc:第一層 = 0x7fe488e14ec0, 第二層 = 0x7fe488e14e50, 第三層element = 0x7fe488e14dd0數組字典混合三層完全拷貝
NSString *elementOne = [NSString stringWithFormat:@"我屮艸芔茻"]; NSDictionary *dic_one = [NSDictionary dictionaryWithObjectsAndKeys:elementOne, @"element", nil]; NSArray *array = [NSArray arrayWithObjects:dic_one, nil];NSMutableArray *arr_mc = [array mutableCopy]; NSMutableArray *arr_mdc = [array mutableDeepCopy];NSLog(@"array->dic: 第一層 = %p, 第二層 = %p, 第三層element = %p",array,array[0],array[0][@"element"]); NSLog(@"arr_mc->dic: 第一層 = %p, 第二層 = %p, 第三層element = %p",arr_mc,arr_mc[0],arr_mc[0][@"element"]); NSLog(@"arr_mdc->dic:第一層 = %p, 第二層 = %p, 第三層element = %p",arr_mdc,arr_mdc[0],arr_mdc[0][@"element"]);打印結果:
array->dic: 第一層 = 0x7fe488d086d0, 第二層 = 0x7fe488da0220, 第三層element = 0x7fe488da1a80 arr_mc->dic: 第一層 = 0x7fe488d94730, 第二層 = 0x7fe488da0220, 第三層element = 0x7fe488da1a80 arr_mdc->dic:第一層 = 0x7fe488d06e00, 第二層 = 0x7fe488d94170, 第三層element = 0x7fe488d14c70完全拷貝總結
根據遞歸原理,關于集合的每個層次都會遍歷到,而且其中的元素也都會遍歷到,也就是每個層次都進行了一個 mutableCopy,這樣在開發中很容易會用到。尤其是在數據源比較復雜,且不可以更改數據源的情況下。
相關資料
assign/retain/copy及深淺拷貝的區別
總結
以上是生活随笔為你收集整理的深拷贝和完全拷贝对比的探究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 心有景旗,志存远方——湖南安全技术职业学
- 下一篇: BetterAndBetter--Mac