看了幾篇文章,因為文章很新手向,所以內容很繁瑣。故整理一下重點,寫了測試程式去瞭解幾個知識點,不討論基本概念。新博客wossoneri.com "傳送門" 非集合類對象的copy與mutableCopy 在非集合類對象中:對 對象進行 操作,是指針複製, 操作時內容複製;對 對象進行 和`mutab ...
看了幾篇文章,因為文章很新手向,所以內容很繁瑣。故整理一下重點,寫了測試程式去瞭解幾個知識點,不討論基本概念。新博客wossoneri.com傳送門
非集合類對象的copy與mutableCopy
//NSString & NSMutableString
NSString *strA = @"origin";
NSLog(@"strA pointer: %p content: %@", strA, strA);
NSString *strACopy = [strA copy];
NSLog(@"strCopy pointer: %p content: %@", strACopy, strACopy);
NSMutableString *strAMCopy = [strA mutableCopy];
NSLog(@"strMCopy pointer: %p content: %@", strAMCopy, strAMCopy);
NSMutableString *strB = [NSMutableString stringWithString:@"mutable string"];
NSLog(@"strB pointer: %p content: %@", strB, strB);
NSMutableString *strBCopy = [strB copy];
NSLog(@"strBCopy pointer: %p content: %@", strBCopy, strBCopy);
NSMutableString *strBMCopy = [strB mutableCopy];
NSLog(@"strBMCopy pointer: %p content: %@", strBMCopy, strBMCopy);
//輸出
strA pointer: 0x100003180 content: origin
strCopy pointer: 0x100003180 content: origin
strMCopy pointer: 0x100102e90 content: origin
strB pointer: 0x100700510 content: mutable string
strBCopy pointer: 0x100700570 content: mutable string
strBMCopy pointer: 0x1006002c0 content: mutable string
在非集合類對象中:對immutable
對象進行copy
操作,是指針複製,mutableCopy
操作時內容複製;對mutable
對象進行copy
和mutableCopy
都是內容複製。
結論
- [immutableObject copy] // 淺複製
- [immutableObject mutableCopy] //深複製
- [mutableObject copy] //深複製
- [mutableObject mutableCopy] //深複製
集合類對象的copy與mutableCopy
NSArray *arrayA = @[@[@"A", @"B"], @[@"C", @"D"]];
NSLog(@"arrayA pointer:%p contentA:%p contentB:%p", arrayA, arrayA[0], arrayA[1]);
NSArray *arrCopyA = [arrayA copy];
NSLog(@"arrCopyA pointer:%p contentA:%p contentB:%p", arrCopyA, arrCopyA[0], arrCopyA[1]);
NSMutableArray *arrMCopyA = [arrayA mutableCopy];
NSLog(@"arrMCopyA pointer:%p contentA:%p contentB:%p", arrMCopyA,arrMCopyA[0], arrMCopyA[1]);
NSMutableArray *arrayB = [NSMutableArray arrayWithObjects:@[@"A", @"B"], @[@"C", @"D"], nil];
NSLog(@"arrayB pointer:%p contentA:%p contentB:%p", arrayB, arrayB[0], arrayB[1]);
NSArray *arrCopyB = [arrayB copy];
NSLog(@"arrCopyB pointer:%p contentA:%p contentB:%p", arrCopyB, arrCopyB[0], arrCopyB[1]);
NSMutableArray *arrMCopyB = [arrayB mutableCopy];
NSLog(@"arrMCopyB pointer:%p contentA:%p contentB:%p", arrMCopyB,arrMCopyB[0], arrMCopyB[1]);
//輸出
arrayA pointer:0x100700860 contentA:0x100700840 contentB:0x1007007c0
arrCopyA pointer:0x100700860 contentA:0x100700840 contentB:0x1007007c0
arrMCopyA pointer:0x100700990 contentA:0x100700840 contentB:0x1007007c0
arrayB pointer:0x100701970 contentA:0x100700880 contentB:0x100701730
arrCopyB pointer:0x100102080 contentA:0x100700880 contentB:0x100701730
arrMCopyB pointer:0x100701ab0 contentA:0x100700880 contentB:0x100701730
可以看到拷貝過程和非集合類大體相同。註意一點,就是拷貝過程只是針對對象本身,而對象內部的元素還是指針複製,從上面列印的輸出就可以看出來。
示例
看如下代碼,有一個mutable
數組,賦值給immutable
數組
@property (nonatomic, strong) NSArray *array;
NSMutableArray *mArray = [NSMutableArray array];
[mArray addObject:@"AA"];
_array = mArray;
NSLog(@"mArray: %p, _array: %p", mArray, _array);
[mArray addObject:@"BB"];
NSLog(@"mArray = %@, _array = %@", mArray, _array);
其中,直接對array
屬性賦值,這樣,_array
和mArray
便是指向同一對象的指針,因為NSMutableArray
是NSArray
的子類,故_array
能夠接受mArray
的值。
輸出結果:
mArray: 0x126ec3e00, _array: 0x126ec3e00
mArray = (
AA,
BB
), _array = (
AA,
BB
)
下麵做個修改,把
_array = mArray;
改為
_array = [mArray copy];
想想結果如何?
mArray
是mutable
對象,拷貝屬於(不完全)深拷貝,所以可以想到_array
的指針和mArray
並不相同,所以後面mArray
改變內容也與_array
無關。
輸出:
mArray: 0x154dc8070, _array: 0x154dcf4e0
mArray = (
AA,
BB
), _array = (
AA
)
小結
Reference
小結 iOS 中的 copy
iOS 集合的深複製與淺複製