copy與mutableCopy
不管是集合類對(duì)象,還是非集合類對(duì)象凫海,接收到copy和mutableCopy消息時(shí)拦键,都遵循以下準(zhǔn)則:
- copy返回imutable對(duì)象;所以毅往,如果對(duì)copy返回值使用mutable對(duì)象接口就會(huì)crash牵咙;
- mutableCopy返回mutable對(duì)象
非集合類
NSString *string = [NSString stringWithString: @"alan.com"];
NSString *stringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
NSLog(@"string = %p, stringCopy = %p, stringMCopy = %p", string, stringCopy, stringMCopy);
輸出結(jié)果來看:
string = 0xa000000000000311
stringCopy = 0xa000000000000311
stringMCopy = 0x608000266f80
輸出地址來看,string與stringCopy一樣攀唯,是指針拷貝洁桌。stringMCopy地址不一樣是內(nèi)容拷貝。
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:@"!!"];
根據(jù)上面的準(zhǔn)則侯嘀。即使是用NSMutableString
聲明的變量另凌,只要是copy方法賦值都是NSString
類型庆揪。所以在[mStringCopy appendString:@"mm"];
會(huì)發(fā)生crash直砂。但是是深拷貝。
string = 0x600000274ac0
stringCopy = 0xa006e696769726f6
mStringCopy = 0xa006e696769726f6 // 兩個(gè)地址一樣
stringMCopy = 0x600000274a80
總結(jié):
- [immutableObject copy] // 淺復(fù)制
- [immutableObject mutableCopy] //深復(fù)制
- [mutableObject copy] //深復(fù)制
- [mutableObject mutableCopy] //深復(fù)制
集合類
先看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]);
}
打印一下輸出結(jié)果:
array = 0x608000033f00
arrayCopy = 0x608000033f00
arrayMCopy = 0x608000243a20
item = 0x608000263940, itemCopy = 0x608000263940, itemMCopy = 0x608000263940
item = 0x1051be120, itemCopy = 0x1051be120, itemMCopy = 0x1051be120
從結(jié)果來看唯袄,array的copy是淺拷貝诗茎,mutableCopy是深拷貝工坊。但是這是對(duì)數(shù)組本來來說的,數(shù)組中的元素都是淺拷貝。
再看看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]);
}
輸出結(jié)果:
array = 0x608000251820
arrayCopy = 0x608000032940
arrayMCopy = 0x608000251c40
item = 0x608000267340, itemCopy = 0x608000267340, itemMCopy = 0x608000267340
item = 0x107e4d110, itemCopy = 0x107e4d110, itemMCopy = 0x107e4d110
從結(jié)果來看王污,NSMutableArray的copy與mutableCopy都是深拷貝罢吃,但是數(shù)組中的元素卻是淺拷貝。
- [immutableObject copy] // 淺復(fù)制
- [immutableObject mutableCopy] //深復(fù)制昭齐,元素淺復(fù)制
- [mutableObject copy] //深復(fù)制尿招,元素淺復(fù)制
- [mutableObject mutableCopy] //深復(fù)制,元素淺復(fù)制
集合的單層深復(fù)制 (one-level-deep copy)
蘋果的開發(fā)文檔中有這樣一句話:
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阱驾。
向上面的例子中泊业,數(shù)組是深拷貝,而元素卻是淺拷貝就是one-level-deep copy啊易。
那怎么才能進(jìn)行集合的深拷貝呢?
NSArray *deepCopyArray=[[NSArray alloc] initWithArray:someArray copyItems:YES];
使用這個(gè)函數(shù)饮睬,設(shè)置最后一個(gè)參數(shù)為YES
租谈。
當(dāng)然還有另外的一種方式,對(duì)集合進(jìn)行歸檔(archive)和解歸檔(unarchive)捆愁,例子就不在這里寫了割去。
注意:
如果在集合中存在NSObject類型的數(shù)據(jù),使用copy與mutableCopy時(shí)昼丑,需要實(shí)現(xiàn)NSCoping與NSMutableCoping協(xié)議呻逆。
@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其實(shí)就是在set方法中進(jìn)行變量的copy操作。
- (void)setCp:(NSString *)cp
{
_cp = [cp copy];
}
而strong則是對(duì)變量的強(qiáng)引用菩帝,引用計(jì)數(shù)+1咖城。但是在ARC下這里導(dǎo)致的引用計(jì)數(shù)變成3。是在setSp:
方法中呼奢,參數(shù)已經(jīng)+1宜雀,然后再一次飲用,最終的引用計(jì)數(shù)變成3握础。MRC下的retainCount是正常的辐董,引用技術(shù)為2。
- (void)setSp:(NSString *)sp
{
NSLog(@"retainCount = %ld", CFGetRetainCount((__bridge CFTypeRef)(sp)));
_sp = sp;
NSLog(@"retainCount = %ld", CFGetRetainCount((__bridge CFTypeRef)(sp)));
}
但是禀综,在任何場(chǎng)合下是不提倡使用retainCount的简烘。傳送門