淺複製:只複製指向對象的指針,而不複製引用對象本身。計數器+1 ,就像比如retain 深層複製:複製引用對象本身。計數器不變,產生新對象 retain:始終是淺複製。引用計數每次加一。返回對象是否可變與被覆制的對象保持一致。 copy:對於可變對象為深複製,引用計數不改變;對於不可變對象是淺複製, ...
淺複製:只複製指向對象的指針,而不複製引用對象本身。計數器+1 ,就像比如retain
深層複製:複製引用對象本身。計數器不變,產生新對象
retain:始終是淺複製。引用計數每次加一。返回對象是否可變與被覆制的對象保持一致。
copy:對於可變對象為深複製,引用計數不改變;對於不可變對象是淺複製,引用計數每次加一。始終返回一個不可變對象。
mutableCopy:始終是深複製,引用計數不改變。始終返回一個可變對象。
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"two day", nil]; NSMutableArray *array2=[array1 copy]; NSArray *array3=[array2 copy]; NSArray *array4=[array2 retain]; NSMutableArray *array5=[array1 retain]; NSMutableArray *array6=[array1 mutableCopy]; NSArray *array7=[array2 mutableCopy]; NSLog(@"-------------"); NSLog(@"array1:%@ %p",array1, array1); NSLog(@"array2:%@ %p",array2, array2); NSLog(@"array3:%@ %p",array3, array3); NSLog(@"array4:%@ %p",array4, array4); NSLog(@"array5:%@ %p",array5, array5); NSLog(@"array6:%@ %p",array6, array6); NSLog(@"array7:%@ %p",array7, array7); NSLog(@"-------------"); return 0; }源代碼
2015-04-01 20:46:23.131 04-01-person[2204:165655] ------------- 2015-04-01 20:46:23.132 04-01-person[2204:165655] array1:( "two day" ) 0x100114570 2015-04-01 20:46:23.133 04-01-person[2204:165655] array2:( "two day" ) 0x100114700 2015-04-01 20:46:23.133 04-01-person[2204:165655] array3:( "two day" ) 0x100114700 2015-04-01 20:46:23.133 04-01-person[2204:165655] array4:( "two day" ) 0x100114700 2015-04-01 20:46:23.133 04-01-person[2204:165655] array5:( "two day" ) 0x100114570 2015-04-01 20:46:23.133 04-01-person[2204:165655] array6:( "two day" ) 0x1001150d0 2015-04-01 20:46:23.134 04-01-person[2204:165655] array7:( "two day" ) 0x100115b00 2015-04-01 20:46:23.134 04-01-person[2204:165655] ------------- Program ended with exit code: 0運行結果
數組類型轉換:
1、不可變對象→可變對象的轉換:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSArray *array01=[NSArray arrayWithObjects:@"zhang",@"wang",@"li", nil]; NSMutableArray *array02=[array01 mutableCopy]; //mutableCopy始終返回一個可變對象 NSLog(@"array01: %@ %p",array01,array01); NSLog(@"array02: %@ %p",array02,array02); return 0; }源代碼
2015-04-01 21:00:28.218 04-01-person[2252:170414] array01: ( zhang, wang, li ) 0x100106c80 2015-04-01 21:00:28.220 04-01-person[2252:170414] array02: ( zhang, wang, li ) 0x1001076d0 Program ended with exit code: 0運行結果
2、可變對象→不可變對象的轉換:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"zhang",@"wang",@"li", nil]; NSArray *array2=[array1 copy];//copy對於可變對象為深複製,引用計數不改變;對於不可變對象是淺複製,引用計數每次加一。始終返回一個不可變對象 NSLog(@"array1: %@ %p",array1,array1); NSLog(@"array2: %@ %p",array2,array2); return 0; }源代碼
2015-04-01 21:03:45.349 04-01-person[2263:171615] array1: ( zhang, wang, li ) 0x100112900 2015-04-01 21:03:45.350 04-01-person[2263:171615] array2: ( zhang, wang, li ) 0x1001133b0運行結果
3、可變對象→可變對象的轉換(不同指針變數指向不同的記憶體地址):
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSMutableArray *array1= [NSMutableArray arrayWithObjects:@"zhang",@"wang",@"li", nil]; NSMutableArray *array2=[array1 mutableCopy]; NSLog(@"array1: %@ %p",array1,array1); NSLog(@"array2: %@ %p",array2,array2); return 0; }源代碼
2015-04-01 20:57:41.732 04-01-person[2243:169571] array1: ( zhang, wang, li ) 0x100114570 2015-04-01 20:57:41.734 04-01-person[2243:169571] array2: ( zhang, wang, li ) 0x100115020 Program ended with exit code: 0運行結果
4、同類型對象之間的指針複製(不同指針變數指向同一塊記憶體地址):
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSArray *array1=[NSArray arrayWithObjects:@"one",@"two",@"three", nil]; NSArray *array2=[array1 copy]; NSLog(@"array1: %@ %p",array1,array1); NSLog(@"array2: %@ %p",array2,array2); return 0; }源代碼
2015-04-01 20:53:07.398 04-01-person[2217:167996] array1: ( one, two, three ) 0x1006069a0 2015-04-01 20:53:07.399 04-01-person[2217:167996] array2: ( one, two, three ) 0x1006069a0 Program ended with exit code: 0運行結果
當多個指針同時指向同一塊記憶體區域,那麼這些個指針同時擁有對該記憶體區的所有權。所有權的瓜分過程,這時候就要用到淺拷貝了。