copy,copy是什麼意思
1) 對於非容器類對象如NSString, NSNumber之類的而言
NSString *str1 = @"abc";
NSString *str2 = [str1 copy]; // 地址和Str1一樣
NSString *str3 = [str1 mutableCopy]; // 地址和str1不同, 雖然是深拷貝,但無法修改str3內容(文法不通過)
NSMutableString *str4 = [str1 copy]; // 地址和Str一樣, 修改Str4會崩潰
NSMutableString *str5 = [str1 mutableCopy]; // 地址和str1 不同
NSLog(@"str1 ===== %p", str1);
NSLog(@"str2 ===== %p", str2);
NSLog(@"str3 ===== %p", str3);
NSLog(@"str4 ===== %p", str4);
NSLog(@"str5 ===== %p", str5);
[str3 appendString:@"xxx"]; // 報錯,語法錯誤
[str4 appendString:@"xxx"]; // 會導致程式崩潰. reason: 'Attempt to mutate immutable object with appendString:'
執行結果:
2015-09-13 22:08:58.958 test[8507:69904] str1 ===== 0x105b407d0
2015-09-13 22:08:58.959 test[8507:69904] str2 ===== 0x105b407d0
2015-09-13 22:08:58.959 test[8507:69904] str3 ===== 0x7fdfc042b150
2015-09-13 22:08:58.959 test[8507:69904] str4 ===== 0x105b407d0
2015-09-13 22:08:58.959 test[8507:69904] str5 ===== 0x7fdfc042ca60
// =============================================
NSMutableString *str1 = [NSMutableString stringWithFormat:@"abc"]; NSString *str2 = [str1 copy]; // 地址和Str1不同 NSString *str3 = [str1 mutableCopy]; // 地址和str1不同, 雖然是深拷貝,但無法修改str3內容(文法不通過) NSMutableString *str4 = [str1 copy]; // 地址和Str1不同, str4本質是NSString而不是NSMutableString, 修改Str4會崩潰 NSMutableString *str5 = [str1 mutableCopy]; // 地址和str1 不同 NSLog(@"str1 ===== %p", str1); NSLog(@"str2 ===== %p", str2); NSLog(@"str3 ===== %p", str3); NSLog(@"str4 ===== %p", str4); NSLog(@"str5 ===== %p", str5);
執行結果:
2015-09-13 22:55:32.116 test[11538:104525] str1 ===== 0x7fab3949b140
2015-09-13 22:55:32.117 test[11538:104525] str2 ===== 0x7fab3949e5d0
2015-09-13 22:55:32.117 test[11538:104525] str3 ===== 0x7fab39490970
2015-09-13 22:55:32.117 test[11538:104525] str4 ===== 0x7fab394975a0
2015-09-13 22:55:32.117 test[11538:104525] str5 ===== 0x7fab3949e470
結論: 對於可變類型的拷貝(copy/mutableCopy)都是深拷貝, 但使用copy返回的對象的真實類型是不可變的, 修改它會導致程式崩潰; 對於不可變類型的copy是淺拷貝, mutableCopy是深拷貝.
2) 對於容器類對象如NSArray, NSDictionary之類的而言
NSArray *array1 = [NSArray arrayWithObjects:[NSMutableString stringWithFormat:@"a"], @"b", @"c", nil];
NSArray *array2 = [array1 copy]; // 地址與array1一樣
NSArray *array3 = [array1 mutableCopy]; // 地址與array1不同
NSMutableArray *array4 = [array1 copy]; // 地址與array1一樣
NSMutableArray *array5 = [array1 mutableCopy]; // 地址與array1不樣
NSArray *array6 = [[NSArray alloc] initWithArray:array1 copyItems:YES]; // 地址與array1不同, 容器內的可變元素深拷貝, 而不可變元素依然是淺拷貝.
NSArray *array7 = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:array1]]; // 地址與array1不同, 容器內的元素都進行深拷貝
NSMutableString *str = array5[0];
[str appendString:@"xxx"];
NSMutableString *str2 = array5[1];
// [str2 appendString:@"xxx"]; // 會導致程式崩潰.
NSLog(@"array1 ==== %p", array1);
NSLog(@"array2 ==== %p", array2);
NSLog(@"array3 ==== %p", array3);
NSLog(@"array4 ==== %p", array4);
NSLog(@"array5 ==== %p", array5);
NSLog(@"array6 ==== %p", array6);
NSLog(@"array7 ==== %p", array7);
NSLog(@"array1[0] ==== %p", array1[0]);
NSLog(@"array2[0] ==== %p", array2[0]);
NSLog(@"array3[0] ==== %p", array3[0]);
NSLog(@"array4[0] ==== %p", array4[0]);
NSLog(@"array5[0] ==== %p", array5[0]);
NSLog(@"array6[0] ==== %p", array6[0]);
NSLog(@"array7[0] ==== %p", array7[0]);
NSLog(@"array1[1] ==== %p", array1[1]);
NSLog(@"array2[1] ==== %p", array2[1]);
NSLog(@"array3[1] ==== %p", array3[1]);
NSLog(@"array4[1] ==== %p", array4[1]);
NSLog(@"array5[1] ==== %p", array5[1]);
NSLog(@"array6[1] ==== %p", array6[1]);
NSLog(@"array7[1] ==== %p", array7[1]);
NSLog(@"array1[0] ==== %@", array1[0]);
NSLog(@"array2[0] ==== %@", array2[0]);
NSLog(@"array3[0] ==== %@", array3[0]);
NSLog(@"array4[0] ==== %@", array4[0]);
NSLog(@"array5[0] ==== %@", array5[0]);
NSLog(@"array6[0] ==== %@", array6[0]);
NSLog(@"array7[0] ==== %@", array7[0]);
執行結果:
2015-09-13 23:15:46.235 test[12791:118074] array1 ==== 0x7fc6e1d1dab0
2015-09-13 23:15:46.236 test[12791:118074] array2 ==== 0x7fc6e1d1dab0
2015-09-13 23:15:46.236 test[12791:118074] array3 ==== 0x7fc6e1d2ef40
2015-09-13 23:15:46.236 test[12791:118074] array4 ==== 0x7fc6e1d1dab0
2015-09-13 23:15:46.236 test[12791:118074] array5 ==== 0x7fc6e1d19430
2015-09-13 23:15:46.236 test[12791:118074] array6 ==== 0x7fc6e1d2fc60
2015-09-13 23:15:46.236 test[12791:118074] array7 ==== 0x7fc6e1c30cd0
2015-09-13 23:15:46.237 test[12791:118074] array1[0] ==== 0x7fc6e1d1ba10
2015-09-13 23:15:46.237 test[12791:118074] array2[0] ==== 0x7fc6e1d1ba10
2015-09-13 23:15:46.237 test[12791:118074] array3[0] ==== 0x7fc6e1d1ba10
2015-09-13 23:15:46.237 test[12791:118074] array4[0] ==== 0x7fc6e1d1ba10
2015-09-13 23:15:46.237 test[12791:118074] array5[0] ==== 0x7fc6e1d1ba10
2015-09-13 23:15:46.237 test[12791:118074] array6[0] ==== 0x7fc6e1d2fc40
2015-09-13 23:15:46.238 test[12791:118074] array7[0] ==== 0x7fc6e1c2faf0
2015-09-13 23:15:46.238 test[12791:118074] array1[1] ==== 0x10fd197f0
2015-09-13 23:15:46.238 test[12791:118074] array2[1] ==== 0x10fd197f0
2015-09-13 23:15:46.238 test[12791:118074] array3[1] ==== 0x10fd197f0
2015-09-13 23:15:46.238 test[12791:118074] array4[1] ==== 0x10fd197f0
2015-09-13 23:15:46.239 test[12791:118074] array5[1] ==== 0x10fd197f0
2015-09-13 23:15:46.239 test[12791:118074] array6[1] ==== 0x10fd197f0
2015-09-13 23:15:46.239 test[12791:118074] array7[1] ==== 0x7fc6e1c2fc50
2015-09-13 23:15:46.239 test[12791:118074] array1[0] ==== axxx
2015-09-13 23:15:46.239 test[12791:118074] array2[0] ==== axxx
2015-09-13 23:15:46.239 test[12791:118074] array3[0] ==== axxx
2015-09-13 23:15:46.240 test[12791:118074] array4[0] ==== axxx
2015-09-13 23:15:46.240 test[12791:118074] array5[0] ==== axxx
2015-09-13 23:15:46.240 test[12791:118074] array6[0] ==== a
2015-09-13 23:15:46.240 test[12791:118074] array7[0] ==== a
結論: 對於非容器類對象的結論同樣適用於容器類對象, 只是容器類對象內的元素始終都只是淺拷貝. 若要對容器內的元素也進行真正意義上的深拷貝, 可以採用歸檔解檔的方式進行.
官方相關連結:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html