网上一堆 copy retain weak assign 模棱两可的描述,越看越令人懵!
知乎大牛解释得非常好:
只要看两篇就可以弄的很懂。
1.copy 和 strong
https://www.zhihu.com/question/20102376
2.深浅复制
https://www.zhihu.com/question/36987710
NSMutableString* str1 = [NSMutableString stringWithString:@"hello"];
NSMutableString* str2 = [NSMutableString stringWithString:@"world"];
NSArray* array = [NSArray arrayWithObjects:str1, str2, nil];
NSArray* arrayShadowCopy = [array copy];
// NSMutableArray* arrayShadowCopy = [array mutableCopy];
// [arrayShadowCopy removeObjectAtIndex:0];
NSMutableArray* arrayDeepCopy = [NSMutableArray arrayWithCapacity:[array count]];
for (NSMutableString* str in array)
{
[arrayDeepCopy addObject:[str copy]];
}
[str1 appendString:@"1234"];
NSLog(@"array: %@ and %p", array, array);
NSLog(@"array shadow copy : %@ and %p", arrayShadowCopy, arrayShadowCopy);
NSLog(@"array deep copy : %@ and %p", arrayDeepCopy, arrayDeepCopy);
搞明白这段代码,和组合各种情况模拟一样,就可以明白,其实说到底了就是 指针 和 内存区的事情啦