一、copy
(1)用copy修饰的 或者赋值的 变量肯定是不可变的。
NSMutableArray *muteArray = [NSMutableArray arrayWithCapacity:10];
[muteArray addObject:@"1"];
NSArray *array = [muteArray copy];
NSLog(@"\n mArr1 = %@ mArr1P = %p mArr1 class = %@ \n\n mArr2 = %@ mArr2P = %p mArr2 class = %@", muteArray, muteArray, [muteArray class], array, array, [array class]);
image.png
(2)用copy赋值,要看源对象是否是可变的,来决定只拷贝指针,还是也拷贝对象到另一块内存空间
NSString *str1 = @"str1";
NSString *str2 = [str1 copy];
NSLog(@"\nstr1 = %@ str1P = %p \n str2 = %@ str2P = %p", str1, str1, str2, str2);
str1 = @"asdf";
NSLog(@"\nstr1 = %@ str1P = %p \n str2 = %@ str2P = %p", str1, str1, str2, str2);
image.png
二、mutableCopy
(1)对象之间mutableCopy赋值,肯定会拷贝整个对象内存到另一块内存中
NSMutableString *mStr1 = [@"123" mutableCopy];
NSMutableString *mStr2 = [mStr1 mutableCopy];
NSLog(@"\n mStr1 = %@ mStr1P = %p \n mStr2 = %@ mStr2P = %p", mStr1, mStr1, mStr2, mStr2);
/*输出结果,可以得出下图结论
mStr1 = 123 mStr1P = 0x6000004460c0
mStr2 = 123 mStr2P = 0x600000446420
*/
网友评论