对象 的copy和mutableCopy
2021-03-05 本文已影响0人
NJKNJK
解释这个问题需要从两个角度去入手
1.NSString
NSString * string = @"123";
NSString * stringCopy = [string copy];
NSString * stringMCopy = [string mutableCopy];
NSLog(@"string--%@ stringCopy--%@ stringMCopy--%@",string,stringCopy,stringMCopy);
NSLog(@"string--%p stringCopy--%p stringMCopy--%p",string,stringCopy,stringMCopy);
NSLog(@"string--%p stringCopy--%p stringMCopy--%p",&string,&stringCopy,&stringMCopy);
NSLog(@"==============================================");
NSMutableString * mString = [NSMutableString stringWithString:@"456"];
NSString * mStringCopy = [mString copy];
NSString * mStringMCopy = [mString mutableCopy];
NSLog(@"mString--%@ mStringCopy--%@ mStringMCopy--%@",mString,mStringCopy,mStringMCopy);
NSLog(@"mString--%p mStringCopy--%p mStringMCopy--%p",mString,mStringCopy,mStringMCopy);
NSLog(@"mString--%p mStringCopy--%p mStringMCopy--%p",&mString,&mStringCopy,&mStringMCopy);
打印结果
2021-03-05 16:45:28.601665+0800 copyDemo[375:33336] string--123 stringCopy--123 stringMCopy--123
2021-03-05 16:45:28.602094+0800 copyDemo[375:33336] string--0x104d14218 stringCopy--0x104d14218 stringMCopy--0x2828b1d70
2021-03-05 16:45:28.602389+0800 copyDemo[375:33336] string--0x16b0f50f8 stringCopy--0x16b0f50f0 stringMCopy--0x16b0f50e8
2021-03-05 16:45:28.602651+0800 copyDemo[375:33336] ==============================================
2021-03-05 16:45:28.603109+0800 copyDemo[375:33336] mString--456 mStringCopy--456 mStringMCopy--456
2021-03-05 16:45:28.603381+0800 copyDemo[375:33336] mString--0x2828b2460 mStringCopy--0xc73940096a246afa mStringMCopy--0x2828b26a0
2021-03-05 16:45:28.603636+0800 copyDemo[375:33336] mString--0x16b0f50e0 mStringCopy--0x16b0f50d8 mStringMCopy--0x16b0f50d0
对于不可变字符串来说
- copy是浅拷贝,mutableCopy为深拷贝。都生成了新的指针
- copy产生的对象只能为不可变,如果强行变为可变字符串,也不具有可变的属性。
- mutableCopy产生的对象可以赋值给可变字符串或者不可变字符串
对于可变字符串来说
- copy和mutableCopy都为深拷贝。都生成了新的指针
- copy和mutableCopy产生的对象都可以赋值给可变字符串或者不可变字符串
2.NSArray
NSArray * array = @[@"1",@"2",@"3"];
NSArray * arrayCopy = [array copy];//就算复制给可变数组,本质也是不可变的
NSMutableArray * arrayMCopy = [array mutableCopy];//因为是创建新的,所以可以变为可变数组。
NSLog(@"array %@",array);
NSLog(@"arrayCopy %@",arrayCopy);
NSLog(@"arrayMCopy %@",arrayMCopy);
NSLog(@"=======================");
NSLog(@"array: %p %p %p %p",array,array[0],array[1],array[2]);
NSLog(@"arrayCopy: %p %p %p %p",arrayCopy,arrayCopy[0],arrayCopy[1],arrayCopy[2]);
NSLog(@"arrayMCopy: %p %p %p %p",arrayMCopy,arrayMCopy[0],arrayMCopy[1],arrayMCopy[2]);
NSLog(@"=======================");
NSLog(@"array %p",&array);
NSLog(@"arrayCopy %p",&arrayCopy);
NSLog(@"arrayMCopy %p",&arrayMCopy);
NSLog(@"==============================================");
NSMutableArray * mArray = [NSMutableArray arrayWithArray:@[@"4",@"5",@"6"]];
NSMutableArray * mArrayCopy = [mArray copy];
NSMutableArray * mArrayMCopy = [mArray mutableCopy];
NSLog(@"mArray %@",mArray);
NSLog(@"mArrayCopy %@",mArrayCopy);
NSLog(@"mArrayMCopy %@",mArrayMCopy);
NSLog(@"=======================");
NSLog(@"mArray: %p %p %p %p",mArray,mArray[0],mArray[1],mArray[2]);
NSLog(@"mArrayCopy: %p %p %p %p",mArrayCopy,mArrayCopy[0],mArrayCopy[1],mArrayCopy[2]);
NSLog(@"mArrayMCopy: %p %p %p %p",mArrayMCopy,mArrayMCopy[0],mArrayMCopy[1],mArrayMCopy[2]);
NSLog(@"=======================");
NSLog(@"mArray %p",&mArray);
NSLog(@"mArrayCopy %p",&mArrayCopy);
NSLog(@"mStringMCopy %p",&mArrayMCopy);
NSLog(@"==============================================");
打印结果
2021-03-05 16:55:01.616230+0800 copyDemo[384:34873] array (
1,
2,
3
)
2021-03-05 16:55:01.616301+0800 copyDemo[384:34873] arrayCopy (
1,
2,
3
)
2021-03-05 16:55:01.616381+0800 copyDemo[384:34873] arrayMCopy (
1,
2,
3
)
2021-03-05 16:55:01.616405+0800 copyDemo[384:34873] =======================
2021-03-05 16:55:01.616429+0800 copyDemo[384:34873] array: 0x281437d80 0x104e082f8 0x104e08318 0x104e08338
2021-03-05 16:55:01.616454+0800 copyDemo[384:34873] arrayCopy: 0x281437d80 0x104e082f8 0x104e08318 0x104e08338
2021-03-05 16:55:01.616478+0800 copyDemo[384:34873] arrayMCopy: 0x281437c60 0x104e082f8 0x104e08318 0x104e08338
2021-03-05 16:55:01.616686+0800 copyDemo[384:34873] =======================
2021-03-05 16:55:01.616717+0800 copyDemo[384:34873] array 0x16b001080
2021-03-05 16:55:01.616739+0800 copyDemo[384:34873] arrayCopy 0x16b001078
2021-03-05 16:55:01.617051+0800 copyDemo[384:34873] arrayMCopy 0x16b001070
2021-03-05 16:55:01.617078+0800 copyDemo[384:34873] ==============================================
2021-03-05 16:55:01.617139+0800 copyDemo[384:34873] mArray (
4,
5,
6
)
2021-03-05 16:55:01.617263+0800 copyDemo[384:34873] mArrayCopy (
4,
5,
6
)
2021-03-05 16:55:01.617301+0800 copyDemo[384:34873] mArrayMCopy (
4,
5,
6
)
2021-03-05 16:55:01.617324+0800 copyDemo[384:34873] =======================
2021-03-05 16:55:01.617344+0800 copyDemo[384:34873] mArray: 0x281437630 0x104e08498 0x104e084b8 0x104e084d8
2021-03-05 16:55:01.617411+0800 copyDemo[384:34873] mArrayCopy: 0x281437540 0x104e08498 0x104e084b8 0x104e084d8
2021-03-05 16:55:01.617480+0800 copyDemo[384:34873] mArrayMCopy: 0x2814375a0 0x104e08498 0x104e084b8 0x104e084d8
2021-03-05 16:55:01.617729+0800 copyDemo[384:34873] =======================
2021-03-05 16:55:01.617754+0800 copyDemo[384:34873] mArray 0x16b001068
2021-03-05 16:55:01.617778+0800 copyDemo[384:34873] mArrayCopy 0x16b001060
2021-03-05 16:55:01.617800+0800 copyDemo[384:34873] mStringMCopy 0x16b001058
2021-03-05 16:55:01.617822+0800 copyDemo[384:34873] ==============================================
对于不可变数组来说
- copy是浅拷贝,mutableCopy为深拷贝(这里为单层深拷贝)。都生成了新的指针
- copy产生的对象只能为不可变,如果强行变为可变数组,也不具有可变的属性。
- mutableCopy产生的对象可以赋值给可变数组或者不可变数组
对于可变数组来说
- copy和mutableCopy都为深拷贝(这里也为单层深拷贝)。都生成了新的指针
- copy和mutableCopy产生的对象都可以赋值给可变字符串或者不可变字符串
但是,我们要如何才能做到真正的深拷贝呢
NSMutableArray * mArray = [NSMutableArray arrayWithArray:@[@"4",@"5",@"6"]];
NSMutableArray * copyMutableArr = [[NSMutableArray alloc] initWithArray:mArray copyItems:YES];
NSLog(@"mArray: %p %p %p %p",mArray,mArray[0],mArray[1],mArray[2]);
NSLog(@"copyMutableArr: %p %p %p %p",copyMutableArr,copyMutableArr[0],copyMutableArr[1],copyMutableArr[2]);
NSLog(@"==============================================");
NSMutableArray *mArrayObjArray = [NSMutableArray arrayWithArray:@[[NSMutableArray arrayWithArray:@[@"1",@"1"]]
,[NSMutableArray arrayWithArray:@[@"2",@"2"]]]];
NSMutableArray * copymArrayObjArray = [[NSMutableArray alloc] initWithArray:mArrayObjArray copyItems:YES];//只需这样创建深拷贝,就是完全深拷贝,但是内部必须位对象
NSLog(@"mArrayObjArray: %p %p %p",mArrayObjArray,mArrayObjArray[0],mArrayObjArray[1]);
NSLog(@"copymArrayObjArray: %p %p %p",copymArrayObjArray,copymArrayObjArray[0],copymArrayObjArray[1]);
打印结果
2021-03-05 17:05:25.598346+0800 copyDemo[389:36360] ==============================================
2021-03-05 17:05:28.436465+0800 copyDemo[389:36360] mArray: 0x280d1ae20 0x104780498 0x104780498 0x104780498
2021-03-05 17:05:28.436905+0800 copyDemo[389:36360] copyMutableArr: 0x280d1d770 0x104780498 0x104780498 0x104780498
2021-03-05 17:05:28.437179+0800 copyDemo[389:36360] ==============================================
2021-03-05 17:05:28.437646+0800 copyDemo[389:36360] mArrayObjArray: 0x280d1dc80 0x280d1d650 0x280d1d350
2021-03-05 17:05:28.438009+0800 copyDemo[389:36360] copymArrayObjArray: 0x280d1d680 0x280374aa0 0x280374880
- 根据苹果开发者文档说明:只有遵守copy协议的数组元素对象才能进行深拷贝,将对象的副本添加到新的数组里面。如果数组元素对象不遵守copy协议,则会进行浅拷贝。
- 我们这里是常量元素的时候,还是单程深拷贝,但是当元素为遵循copy协议的对象时,达成了真正的深拷贝效果。