理解iOS中的copy与mutableCopy

2017-12-07 12:52:15来源:oschina作者:风了个1人点击

分享

什么是浅拷贝,什么是深拷贝


浅拷贝:不拷贝对象本身,仅仅是拷贝指向对象的指针 深拷贝:是直接拷贝整个对象内存到另一块内存中


一.为什么要有copy与mutableCopy,系统尽可能的节省内存开销


二.实验


1.普通对象的copy与mutableCopy


实例:


// 测试不可变对象的copy
NSString *string = @"aaa";
NSString *copyString = [string copy];
NSLog(@"string ===%p copyString = %p",string,copyString);
// 运行结果test_Copy[21354:420763] string ===0x108728068 copyString = 0x108728068
// 以上说明copy对不可变对象是浅拷贝
NSLog(@"=============/n");// 测试不可变对象的mutableCopy
NSString *mutableCopyString = [string mutableCopy];
NSLog(@"string ===%p mutableCopyString = %p",string,mutableCopyString);
// 运行结果:test_Copy[21354:420763] string ===0x108728068 mutableCopyString = 0x60400005fc20
// 以上说明mutableCopy对不可变对象是深拷贝NSLog(@"=============/n");
// 测试可变对象的copy
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"bbb"];
NSString *copyMutableString = [mutableString copy];
NSLog(@"mutableString ===%p mutableStringCopy = %p",mutableString,copyMutableString);
// 运行结果: test_Copy[21354:420763] mutableString ===0x60000005e360 mutableStringCopy = 0xa000000006262623
// 以上说明copy对可变对象是深拷贝 NSLog(@"=============/n");
// 测试可变对象的mutableCopy
NSString *mutableCopymutableString = [mutableString mutableCopy];
NSLog(@"mutableString ===%p mutableStringMutableCopy = %p",mutableString,mutableCopymutableString);
// 运行结果 test_Copy[21524:427609] mutableString ===0x60400004ed30 mutableStringMutableCopy = 0x60c000055900
// 以上说明mutableCopy对可变对象是深拷贝

2.容器对象的copy与mutableCopy


实例:


Person *person1 = [Person new];
person1.name = @"张三";Person *person2 = [Person new];
person2.name = @"李四";// 测试不可变容器对象的copy
NSArray *array = @[person1,person2];
NSArray *copyArray = [array copy];
NSLog(@"array ==%p array ===%@ /n copyArray = %p copyArray = %@",array,array,copyArray,copyArray);
// 运行结果:
/* array ==0x6000000393c0 array ===(
"",
""
)
copyArray = 0x6000000393c0 copyArray = (
"",
""
) */
// copy 对不可变容器对象作用后,对象指针地址没有变化,容器内元素地址没变
// 以上说明copy对不可变容器对象是浅拷贝,NSLog(@"=============/n");// 测试不可变容器对象的mutableCopy
NSArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array ==%p array ===%@ /n mutableCopyArray = %p mutableCopyArray = %@",array,array,mutableCopyArray,mutableCopyArray);
// 运行结果:
/*
test_Copy[23761:495549] array ==0x60400003df00 array ===(
"",
""
)
mutableCopyArray = 0x604000053080 mutableCopyArray = (
"",
""
) */
// mutableCopy 对不可变容器作用后,开辟了一块心得内存,容器内元素地址没变NSLog(@"=============/n");// 测试可变容器对象的copy
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];
NSArray *copyMutableArray = [mutableArray copy];
NSLog(@"mutableArray ==%p mutableArray ===%@ /n copyMutableArray = %p copyMutableArray = %@",mutableArray,mutableArray,copyMutableArray,copyMutableArray);
// 运行结果:
/*test_Copy[23970:503539] mutableArray ==0x600000440db0 mutableArray ===(
"",
""
)
copyMutableArray = 0x60000003db00 copyMutableArray = (
"",
""
) */
// copy 对可变容器作用后,开辟了一块新得内存,容器内元素地址没变// 测试可变容器对象的mutableCopy
NSArray *mutableCopyMutableArray = [mutableArray mutableCopy];
NSLog(@"mutableArray ==%p mutableArray ===%@ /n mutableCopyMutableArray = %p mutableCopyMutableArray = %@",mutableArray,mutableArray,mutableCopyMutableArray,mutableCopyMutableArray);// 运行结果:
/*test_Copy[24128:510612] mutableArray ==0x6000002409c0 mutableArray ===(
"",
""
)
mutableCopyMutableArray = 0x608000248d00 mutableCopyMutableArray = (
"",
""
)*/// mutableCopy对可变容器作用后,开辟了一块新得内存,容器内元素地址没变

总结:


对于不可变对象,copy是浅拷贝,mutableCopy是深拷贝。


对于可变对象,无论是copy还是mutableCopy都是深拷贝


如果是容器类的对象,比如NSArray,NSMutableArray,无论是深拷贝还是钱拷贝,容器内元素的指针总是拷贝之前的指针,也就是元素只进行了浅拷贝




不可变对象
可变对象
copy
浅拷贝
深拷贝


mutableCopy
深拷贝
深拷贝


上面所说的容器类对象无论是浅拷贝还会深拷贝,容器内元素都是浅拷贝,那么如何才能让容器里面的对象都会深拷贝?


NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];

从上面例子可以看出来,这句代码执行后,array和mutableArray里面元素的地址都不一样


相关文章

    无相关信息

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台