概念
在Objective-C中并不是所有的對象都支持Copy,MutableCopy,遵守NSCopying
協議的類才可以發送Copy消息,遵守NSMutableCopying
協議的類才可以發送MutableCopy消息.
關于對象拷貝有兩種方式:淺拷貝(Shallow Copy)和深拷貝(Deep Copy)。顧名思義,淺拷貝,并不拷貝對象本身,僅僅是拷貝指向對象的指針;深拷貝是直接拷貝整個對象內存到另一塊內存中。
如下圖:
再簡單些說:淺拷貝就是指針拷貝;深拷貝就是內容拷貝。
淺拷貝
這里有幾種方法可以進行淺拷貝,當你創建淺拷貝的時候,在原始集合里的對象就會發送retain消息,引用計數器加1,然后再拷貝指針到新的集合對象中。
NSArray *shallowCopyArray = [someArray copyWithZone:nil];
NSDictionary *shallowCopyDict = [[NSDictionary alloc] initWithDictionary:someDictionary copyItems:NO];
深拷貝
深拷貝有兩種方法,
- 一種是
initWithArray:copyItems:
,將第二個參數設置為YES,則可以執行深拷貝,如:
NSDictionary shallowCopyDict = [[NSDictionary alloc] initWithDictionary:someDictionary copyItems:YES];
NSArray array = [[NSArray alloc] initWithArray:someArray copyItems:YES];
如果使用這種方式進行深拷貝的話,則集合里每個對象元素都會收到copyWithZone:
的消息,對象元素必須遵循NSCopying
協議,如果沒有遵循NSCopying
協議的話,則運行時會發生報錯。
- 第二個方法是將集合進行歸檔(archive),然后解檔(unarchive),如:
NSArray *trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:oldArray]];
這里需要注意:
- 第一種方式
copyWithZone:
這種拷貝方式只能夠提供一層內存拷貝(one-level-deep copy),而非真正的深拷貝。 - 第二種方式歸檔和解檔才是實現真正的深拷貝。
集合單層深拷貝(one-level-deep copy)
看到這里,有同學會問:如果在多層數組中,對第一層進行內容拷貝,其它層進行指針拷貝,這種情況是屬于深拷貝,還是淺拷貝?對此,蘋果官網文檔有這樣一句話描述
This kind of copy is only capable of producing a one-level-deep copy. If you only need a one-level-deep copy...
If you need a true deep copy, such as when you have an array of arrays,you can archive and then unarchive the collection
關于淺拷貝、深拷貝、單層深拷貝做了概念區分:
- 淺拷貝(shallow copy): 在淺拷貝操作時,對于被拷貝對象的每一層都是指針拷貝。
- 深拷貝(one-level-deep copy):在深拷貝操作時,對于被拷貝對象,至少有一層是深拷貝。
- 完全拷貝(real-deep copy):在完全拷貝操作時,對于被拷貝對象的每一層都是對象拷貝。
當然,這些都是概念性的東西,沒有必要糾結于此。只要知道進行拷貝操作時,被拷貝的是指針還是內容即可。
淺拷貝實驗
首先創建Person.h
和Person.m
,記得需要實現<NSCopying>
協議
#Person.h
@interface Person : NSObject <NSCopying>
@property (nonatomic,copy) NSString *name;
@end
#Person.m
@implementation Person
@synthesize name;
//實現copyWithZone方法
- (id)copyWithZone:(NSZone *)zone {
Person *p = [[self class] allocWithZone:zone];
p.name = [self name];
return p;
}
@end
淺拷貝 測試代碼如下:
//創建Person對象
Person *person = [[Person alloc] init];
[person setName:@"leo"];
NSArray *array1 = [[NSArray alloc] initWithObjects:person, nil];
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
[p setName:@"lily"];//嘗試更改name的值
//獲取兩個數組里的各自Person對象
Person *p1 = [array1 objectAtIndex:0];
Person *p2 = [array2 objectAtIndex:0];
NSLog(@"array1:%p",array1);
NSLog(@"array2:%p",array2);
NSLog(@"p1:%p",p1);
NSLog(@"p2:%p",p2);
NSLog(@"p1.name:%@",p1.name);
NSLog(@"p2.name:%@",p2.name);
//打印結果
2016-04-07 03:14:46.964 test[29178:5181526] array1:0x7fd91a644420
2016-04-07 03:14:46.964 test[29178:5181526] array2:0x7fd91a657ed0
2016-04-07 03:14:46.964 test[29178:5181526] p1:0x7fd91a6a04d0
2016-04-07 03:14:46.964 test[29178:5181526] p2:0x7fd91a6a04d0
2016-04-07 03:14:46.964 test[29178:5181526] p1.name:lily
2016-04-07 03:14:46.964 test[29178:5181526] p2.name:lily
從打印出來的Log可以看到array1
和array2
的地址發生改變,但在兩個數組內的Person
對象是指向同一個內存地址,當name
的值發生改變,兩個數組內的Person
對象的name
同時也發生改變。
深拷貝實驗
深拷貝 測試代碼如下
//創建Person對象
Person *person = [[Person alloc] init];
[person setName:@"leo"];
NSArray *array1 = [[NSArray alloc] initWithObjects:person, nil];
//注意僅僅是這里更改為了[[NSArray alloc] initWithArray:array1 copyItems:YES];
NSArray *array2 = [[NSArray alloc] initWithArray:array1 copyItems:YES];
[p setName:@"lily"];//嘗試更改name的值
//獲取兩個數組里的各自Person對象
Person *p1 = [array1 objectAtIndex:0];
Person *p2 = [array2 objectAtIndex:0];
NSLog(@"array1:%p",array1);
NSLog(@"array2:%p",array2);
NSLog(@"p1:%p",p1);
NSLog(@"p2:%p",p2);
NSLog(@"p1.name:%@",p1.name);
NSLog(@"p2.name:%@",p2.name);
//打印結果
2016-04-07 03:27:38.173 test[29303:5192608] array1:0x7fa7e8442200
2016-04-07 03:27:38.173 test[29303:5192608] array2:0x7fa7e8442220
2016-04-07 03:27:38.173 test[29303:5192608] p1:0x7fa7e84815c0
2016-04-07 03:27:38.173 test[29303:5192608] p2:0x7fa7e847d490
2016-04-07 03:27:38.173 test[29303:5192608] p1.name:lily
2016-04-07 03:27:38.174 test[29303:5192608] p2.name:leo
從Log我們可以看出,不但array1
和array2
的內存地址發生改變,連array2
里面的Person
對象也發生改變,生成了另外一個Person
對象,即使屬于array1
的Person
對象的元素name
發生改變也不會影響到array2
里的Person
對象。
關于對象屬性的修飾符"copy"與"strong"
關于對象屬性的修飾符copy
與strong
的不同,以下會舉例子說明:
#Person.h
@interface Person : NSObject <NSCopying>
@property (nonatomic,copy) NSString *name;//這里我們定義為copy
@end
//創建一個可變字符串對象
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"hello"];
Person *p = [[Person alloc] init];
[p setName:mutableString];
[mutableString appendString:@" world!"];//更改mutableString的內容
NSLog(@"p.name:%@",p.name);
//打印結果
2016-04-07 03:49:02.828 test[29679:5209081] p.name:hello
這里我們發現當可變字符串mutableString
改變內容的時候,name
沒有發生改變,說明這里是的copy
將mutableString
進行"內容拷貝"
#Person.h
@interface Person : NSObject <NSCopying>
@property (nonatomic,strong) NSString *name;//這里我們定義為strong
@end
//創建一個可變字符串對象
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"hello"];
Person *p = [[Person alloc] init];
[p setName:mutableString];
[mutableString appendString:@" world!"];//更改mutableString的內容
NSLog(@"p.name:%@",p.name);
//打印結果
2016-04-07 03:49:02.828 test[29679:5209081] p.name:hello world!
當將copy
改為strong
后發現可變字符串mutableString
更改內容后,會直接影響到name
的值,說明strong
是對mutableString
進行指針復制,然后引用計數器加1
最后我們得出的結論
一般情況下對象屬性NSString的修飾符設置為copy,因為這樣是防止可變字符串更改自身的值的時候不會影響到對象屬性NSString的值
其實不只是NSString,包括NSArray,NSDictionary都建議設置成copy
系統對象的copy與mutableCopy方法
不管是集合類對象,還是非集合類對象,接收到copy和mutableCopy消息時,都遵循以下準則:
- copy返回imutable對象;所以,如果對copy返回值使用mutable對象接口就會crash;
- mutableCopy返回mutable對象;
下面將針對非集合類對象和集合類對象的copy和mutableCopy方法進行具體的闡述
1、非集合類對象的copy與mutableCopy
系統非集合類對象指的是 NSString, NSNumber ... 之類的對象。下面先看個非集合類immutable對象拷貝的例子
NSString *string = @"origin";
NSString *stringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
通過查看內存,可以看到 stringCopy 和 string 的地址是一樣,進行了指針拷貝;而 stringMCopy 的地址和 string 不一樣,進行了內容拷貝;
再看mutable對象拷貝例子
NSMutableString *string = [NSMutableString stringWithString: @"origin"];
//copy
NSString *stringCopy = [string copy];
NSMutableString *mStringCopy = [string copy];
NSMutableString *stringMCopy = [string mutableCopy];
//change value
[mStringCopy appendString:@"mm"]; //crash
[string appendString:@" origion!"];
[stringMCopy appendString:@"!!"];
運行以上代碼,會在第7行crash,原因就是 copy 返回的對象是 immutable 對象。注釋第7行后再運行,查看內存,發現 string、stringCopy、mStringCopy、stringMCopy 四個對象的內存地址都不一樣,說明此時都是做內容拷貝。
綜上兩個例子,我們可以得出結論:
在非集合類對象中:對immutable對象進行copy操作,是指針拷貝,mutableCopy操作時內容拷貝;對mutable對象進行copy和mutableCopy都是內容拷貝。用代碼簡單表示如下:
- [immutableObject copy] // 淺拷貝
- [immutableObject mutableCopy] //深拷貝
- [mutableObject copy] //深拷貝
- [mutableObject mutableCopy] //深拷貝
2、集合類對象的copy與mutableCopy
集合類對象是指NSArray、NSDictionary、NSSet ... 之類的對象。下面先看集合類immutable對象使用copy和mutableCopy的一個例子:
NSArray *array = @[@[@"a", @"b"], @[@"c", @"d"];
NSArray *copyArray = [array copy];
NSMutableArray *mCopyArray = [array mutableCopy];
查看內容,可以看到copyArray和array的地址是一樣的,而mCopyArray和array的地址是不同的。說明copy操作進行了指針拷貝,mutableCopy進行了內容拷貝。但需要強調的是:此處的內容拷貝,僅僅是拷貝array這個對象,array集合內部的元素仍然是指針拷貝。這和上面的非集合immutable對象的拷貝還是挺相似的,那么mutable對象的拷貝會不會類似呢?我們繼續往下,看mutable對象拷貝的例子:
NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"a"],@"b",@"c",nil];
NSArray *copyArray = [array copy];
NSMutableArray *mCopyArray = [array mutableCopy];
查看內存,如我們所料,copyArray、mCopyArray和array的內存地址都不一樣,說明copyArray、mCopyArray都對array進行了內容拷貝。同樣,我們可以得出結論:
在集合類對象中,對immutable對象進行copy,是指針拷貝,mutableCopy是內容拷貝;對mutable對象進行copy和mutableCopy都是內容拷貝。但是:集合對象的內容拷貝僅限于對象本身,對象元素仍然是指針拷貝。用代碼簡單表示如下:
- [immutableObject copy] // 淺拷貝
- [immutableObject mutableCopy] //單層深拷貝
- [mutableObject copy] //單層深拷貝
- [mutableObject mutableCopy] //單層深拷貝