Copy & MutableCopy
最近发现深浅拷贝居然记不清了,特地来温习下。
测试代码
- (void)testDictionary {
NSDictionary *dicImmutable = [NSDictionary dictionaryWithObjectsAndKeys:@"lhe", @"name", nil];
// NSDictionary *dicImmutable = @{@"name":@"lhe"};
NSMutableDictionary *dicMutable = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"18", @"age", nil];
NSMutableDictionary *dicCopy = [dicMutable copy];
NSMutableDictionary *dicMutableCopy = [dicMutable mutableCopy];
NSLog(@"类型:\n dicImmutable = %@, dicCopy = %@, dicMutableCopy = %@, dicMutable = %@", [dicImmutable class], [dicCopy class], [dicMutableCopy class], [dicMutable class]);
NSLog(@"指针:\n dicImmutable = %p, dicCopy = %p, dicMutableCopy = %p, dicMutable = %p", dicImmutable, dicCopy, dicMutableCopy, dicMutable);
NSLog(@"dicMutable.value = %@, dicCopy.value = %@, dicMutableCopy.value = %@", dicMutable[@"age"], dicCopy[@"age"], dicMutableCopy[@"age"]);
NSLog(@"dicMutable.value = %p, dicCopy.value = %p, dicMutableCopy.value = %p", dicMutable[@"age"], dicCopy[@"age"], dicMutableCopy[@"age"]);
// [dicImmutable setValue:@"dfsdf" forKey:@"name"];
}
- (void)testArray {
NSArray *arrImmutable = [NSArray arrayWithObjects:@"hello", @"world", nil];
NSMutableArray *arrMutable = [NSMutableArray arrayWithObjects:@"s", @"d", @"r", nil];
NSMutableArray *arrCopy = [arrMutable copy];
NSMutableArray *arrMutableCopy = [arrMutable mutableCopy];
NSLog(@"类型:\n arrImmutable = %@, arrCopy = %@, arrMutableCopy = %@, arrMutable = %@", [arrImmutable class], [arrCopy class], [arrMutableCopy class], [arrMutable class]);
NSLog(@"指针:\n arrImmutable = %p, arrCopy = %p, arrMutableCopy = %p, arrMutable = %p", arrImmutable, arrCopy, arrMutableCopy, arrMutable);
// [arr2 addObject:@"lhe"];
// [arr3 addObject:@"lh"];
}
- (void)testString {
NSString *nameImmutable = @"slg";//[NSString stringWithFormat:@"%@", @"slj"];
NSMutableString *nameMutable = [NSMutableString stringWithString:@"lhe"];
NSMutableString *nameCopy = [nameImmutable copy];
NSMutableString *nameMutableCopy = [nameImmutable mutableCopy];
NSLog(@"类型:\n nameImmutable = %@, nameCopy = %@, nameMutableCopy = %@, nameMutable = %@", [nameImmutable class], [nameCopy class], [nameMutableCopy class], [nameMutable class]);
NSLog(@"指针:\n nameImmutable = %p, nameCopy = %p, nameMutableCopy = %p, nameMutable = %p", nameImmutable, nameCopy, nameMutableCopy, nameMutable);
// [nameCopy appendString:@"++"];
// NSLog(@"修改后的name == %@", nameCopy);
}
结果
原类型 | 是否可变/指针/类型 | copy后是否可变/指针/类型 | mutableCopy后是否可变/指针/类型 |
---|---|---|---|
@“str” | ❌/0x10c72c108/__NSCFConstantString | ❌/0x10c72c108/__NSCFConstantString | ✅/0x600001edd230/__NSCFString |
NSString | ❌/0xf7294a46a67d6aa2/NSTaggedPointerString | ❌/0xf7294a46a67d6aa2/NSTaggedPointerString | ✅/0x6000007c86c0/__NSCFString |
NSMutableString | ✅/0x60000165fc60/NSTaggedPointerString | ✅/0xd0246359f685d463/NSTaggedPointerString | ✅/0x60000165fc90/__NSCFString |
NSArray和@[] | ❌/0x6000015417e0/__NSArrayI | ❌/0x6000015417e0/__NSArrayI | ✅/0x600001b4e490/__NSArrayM |
NSMutableArray | ✅/0x600001a94e40/__NSArrayM | ❌/0x600001a94e10/__NSArrayI | ✅/0x600001a94de0/__NSArrayM |
@{} | ❌/0x60000103c1e0/__NSSingleEntryDictionaryI | ❌/0x60000103c1e0/__NSSingleEntryDictionaryI | ✅/0x60000103cb40/__NSDictionaryM |
NSDictionary | ❌/0x600002ee10c0/__NSDictionaryI | ❌/0x600002ee10c0/__NSDictionaryI | ✅/0x600003beb9a0/__NSDictionaryM |
NSMutableDictionary | ✅/0x600000477320/__NSDictionaryM | ✅/0x600000477340/__NSFrozenDictionaryM | ✅/0x600000477360/__NSDictionaryM |
原类型 | 是否可变/类型 | copy后是否可变/指针/类型 | mutableCopy后是否可变/指针/类型 |
---|---|---|---|
@“str” | ❌/__NSCFConstantString | ❌/指针拷贝/__NSCFConstantString | ✅/内容拷贝/__NSCFString |
NSString | ❌/NSTaggedPointerString | ❌/指针拷贝/NSTaggedPointerString | ✅/内容拷贝/__NSCFString |
NSMutableString | ✅/NSTaggedPointerString | ✅/内容拷贝/NSTaggedPointerString | ✅/内容拷贝/__NSCFString |
NSArray和@[] | ❌/__NSArrayI | ❌/指针拷贝/__NSArrayI | ✅/内容拷贝/__NSArrayM |
NSMutableArray | ✅/__NSArrayM | ❌/内容拷贝/__NSArrayI | ✅/内容拷贝/__NSArrayM |
@{} | ❌/__NSSingleEntryDictionaryI | ❌/指针拷贝/__NSSingleEntryDictionaryI | ✅/内容拷贝/__NSDictionaryM |
NSDictionary | ❌/__NSDictionaryI | ❌/指针拷贝/__NSDictionaryI | ✅/内容拷贝/__NSDictionaryM |
NSMutableDictionary | ✅/__NSDictionaryM | ✅/内容拷贝/__NSFrozenDictionaryM | ✅/内容拷贝/__NSDictionaryM |
总结
具体的类型跟创建的方式有关。
- 不可变类型
-
copy
是指针拷贝
,不可变类型
。 -
mutableCopy
是内容拷贝
,变成了可变类型
。
-
- 可变类型
-
copy
是内容拷贝
,可变类型
。 -
mutableCopy
是内容拷贝
,可变类型
。
-
- 只有不可变类型的
copy
是浅拷贝,结果仍为不可变类型
。- 不可变类型的
mutableCopy
,和可变类型的copy
与mutableCopy
都是深拷贝,结果都是可变类型
。- 容器类的深拷贝是这个容器本身的复制,其内元素仍然是
指针复制
。- 使用归解档可以实现真正的深拷贝。
网友评论