copy与mutableCopy
不管是集合类对象,还是非集合类对象,接收到copy和mutableCopy消息时,都遵循以下准则:
- copy返回imutable对象;所以,如果对copy返回值使用mutable对象接口就会crash;
- mutableCopy返回mutable对象
非集合类
NSString *string = [NSString stringWithString: @"alan.com"];
NSString *stringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
NSLog(@"string = %p, stringCopy = %p, stringMCopy = %p", string, stringCopy, stringMCopy);
输出结果来看:
string = 0xa000000000000311
stringCopy = 0xa000000000000311
stringMCopy = 0x608000266f80
输出地址来看,string与stringCopy一样,是指针拷贝。stringMCopy地址不一样是内容拷贝。
NSMutableString *string = [NSMutableString stringWithString: @"alan.com"];
//copy
NSString *stringCopy = [string copy];
NSMutableString *mStringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
//change value
[mStringCopy appendString:@"mm"]; //crash
[string appendString:@" origion!"];
[stringMCopy appendString:@"!!"];
根据上面的准则。即使是用NSMutableString
声明的变量,只要是copy方法赋值都是NSString
类型。所以在[mStringCopy appendString:@"mm"];
会发生crash。但是是深拷贝。
string = 0x600000274ac0
stringCopy = 0xa006e696769726f6
mStringCopy = 0xa006e696769726f6 // 两个地址一样
stringMCopy = 0x600000274a80
总结:
- [immutableObject copy] // 浅复制
- [immutableObject mutableCopy] //深复制
- [mutableObject copy] //深复制
- [mutableObject mutableCopy] //深复制
集合类
先看NSArray:
NSArray *array = @[[NSMutableString stringWithString: @"alan.com"], @"2"];
NSArray *arrayCopy = [array copy];
NSArray *arrayMCopy = [array mutableCopy];
NSLog(@"array = %p", array);
NSLog(@"arrayCopy = %p", arrayCopy);
NSLog(@"arrayMCopy = %p", arrayMCopy);
for (int i = 0; i < [array count]; i ++) {
NSLog(@"item = %p, itemCopy = %p, itemMCopy = %p", array[i], arrayCopy[i], arrayMCopy[i]);
}
打印一下输出结果:
array = 0x608000033f00
arrayCopy = 0x608000033f00
arrayMCopy = 0x608000243a20
item = 0x608000263940, itemCopy = 0x608000263940, itemMCopy = 0x608000263940
item = 0x1051be120, itemCopy = 0x1051be120, itemMCopy = 0x1051be120
从结果来看,array的copy是浅拷贝,mutableCopy是深拷贝。但是这是对数组本来来说的,数组中的元素都是浅拷贝。
再看看NSMutaleArray
NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"alan.com"], @"2", nil];
NSMutableArray *arrayCopy = [array copy];
NSMutableArray *arrayMCopy = [array mutableCopy];
NSLog(@"array = %p", array);
NSLog(@"arrayCopy = %p", arrayCopy);
NSLog(@"arrayMCopy = %p", arrayMCopy);
for (int i = 0; i < [array count]; i ++) {
NSLog(@"item = %p, itemCopy = %p, itemMCopy = %p", array[i], arrayCopy[i], arrayMCopy[i]);
}
输出结果:
array = 0x608000251820
arrayCopy = 0x608000032940
arrayMCopy = 0x608000251c40
item = 0x608000267340, itemCopy = 0x608000267340, itemMCopy = 0x608000267340
item = 0x107e4d110, itemCopy = 0x107e4d110, itemMCopy = 0x107e4d110
从结果来看,NSMutableArray的copy与mutableCopy都是深拷贝,但是数组中的元素却是浅拷贝。
- [immutableObject copy] // 浅复制
- [immutableObject mutableCopy] //深复制,元素浅复制
- [mutableObject copy] //深复制,元素浅复制
- [mutableObject mutableCopy] //深复制,元素浅复制
集合的单层深复制 (one-level-deep copy)
苹果的开发文档中有这样一句话:
However, copyWithZone: produces a shallow copy. This kind of copy is only capable of producing a one-level-deep copy. If you only need a one-level-deep copy, you can explicitly call for one as in Listing 2.
这里也就引入了一种新的概念:one-level-deep copy。
向上面的例子中,数组是深拷贝,而元素却是浅拷贝就是one-level-deep copy。
那怎么才能进行集合的深拷贝呢?
NSArray *deepCopyArray=[[NSArray alloc] initWithArray:someArray copyItems:YES];
使用这个函数,设置最后一个参数为YES
。
当然还有另外的一种方式,对集合进行归档(archive)和解归档(unarchive),例子就不在这里写了。
注意:
如果在集合中存在NSObject类型的数据,使用copy与mutableCopy时,需要实现NSCoping与NSMutableCoping协议。
@property
@property (nonatomic, copy) NSString *cp;
@property (nonatomic, strong) NSString *sp;
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"alan.com"];
NSLog(@"str = %p, retainCount = %ld", str, CFGetRetainCount((__bridge CFTypeRef)(str)));
self.cp = str;
NSLog(@"cp = %p, retainCount = %ld", self.cp, CFGetRetainCount((__bridge CFTypeRef)(str)));
self.sp = str;
NSLog(@"sp = %p, retainCount = %ld", self.sp, CFGetRetainCount((__bridge CFTypeRef)(str)));
str = 0x60000027c8c0, retainCount = 1
cp = 0xa0020220b1ce0c68, retainCount = 1
sp = 0x60000027c8c0, retainCount = 3
copy其实就是在set方法中进行变量的copy操作。
- (void)setCp:(NSString *)cp
{
_cp = [cp copy];
}
而strong则是对变量的强引用,引用计数+1。但是在ARC下这里导致的引用计数变成3。是在setSp:
方法中,参数已经+1,然后再一次饮用,最终的引用计数变成3。MRC下的retainCount是正常的,引用技术为2。
- (void)setSp:(NSString *)sp
{
NSLog(@"retainCount = %ld", CFGetRetainCount((__bridge CFTypeRef)(sp)));
_sp = sp;
NSLog(@"retainCount = %ld", CFGetRetainCount((__bridge CFTypeRef)(sp)));
}
但是,在任何场合下是不提倡使用retainCount的。传送门