国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

copy

2019-11-14 18:19:57
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉祥县| 白山市| 周宁县| 惠来县| 万荣县| 太原市| 广平县| 佛冈县| 密山市| 红河县| 麦盖提县| 和硕县| 太谷县| 额济纳旗| 伊川县| 和硕县| 清新县| 赣州市| 镇坪县| 桓仁| 甘泉县| 平远县| 武义县| 蓝田县| 湖口县| 右玉县| 石城县| 昭平县| 金昌市| 广灵县| 黑河市| 宝应县| 屏山县| 巴青县| 乐东| 武功县| 广西| 祁东县| 夏河县| 佛坪县| 富平县|