首先,聲明
@property(nonatomic,copy)NSString * str1; //不可變字符串
@property(nonatomic,copy)NSMutableString * str2;//可變字符串
// %p ,_str1 取地址 %p,&_str1 取指針地址
NSLog(@"str1 = %@ value = %p",_str1,_str1);
NSLog(@"str1 = %@ copy value = %p",[_str1 copy],[_str1 copy]);
NSLog(@"str1 = %@ mutableCopy value= %p",[_str1 mutableCopy],[_str1 mutableCopy]);
NSLog(@"str2 = %@ value = %p",_str2,_str2);
NSLog(@"str2 = %@ copy value = %p",[_str2 copy],[_str2 copy]);
NSLog(@"str2 = %@ mutableCopy value= %p",[_str2 mutableCopy],[_str2 mutableCopy]);
NSMutableString *str4 = [NSMutableString stringWithString:@"hello"];
NSString *str5 = [str4 copy];
NSString * str6 = str4;
NSLog(@"%@ , %p",str4,str4);
[str4 appendString:@"world"];
NSLog(@"%@ , %p",str4,str4);
NSLog(@"%@ , %p",str5,str5);
NSLog(@"%@ , %p",str6,str6);
內(nèi)存地址都不一樣辟拷,說明此時都是做內(nèi)容拷貝撞羽、深拷貝
**2016-08-12 10:52:38.390 Copy[1794:93551] str1 = 1 value = 0x1099c3068**
**2016-08-12 10:52:38.390 Copy[1794:93551] str1 = 1 copy value = 0x1099c3068**
**2016-08-12 10:52:38.390 Copy[1794:93551] str1 = 1 mutableCopy value= 0x7fb69ac08ba0**
**2016-08-12 10:52:38.390 Copy[1794:93551] str2 = 1 value = 0x7fb69ac093c0**
**2016-08-12 10:52:38.390 Copy[1794:93551] str2 = 1 copy value = 0xa000000000000311**
**2016-08-12 10:52:38.391 Copy[1794:93551] str2 = 1 mutableCopy value= 0x7fb69ae08260**
**2016-08-12 10:52:38.391 Copy[1794:93551] hello , 0x7fb69ad08090**
**2016-08-12 10:52:38.391 Copy[1794:93551] helloworld , 0x7fb69ad08090**
**2016-08-12 10:52:38.391 Copy[1794:93551] hello , 0xa00006f6c6c65685**
**2016-08-12 10:52:38.391 Copy[1794:93551] helloworld , 0x7fb69ad08090**
**2016-08-12 10:52:38.391 Copy[1794:93551] str1 = 1 , str2 = 1**
- 對非集合類對象的copy操作:
在非集合類對象中:對 immutable 對象進行 copy 操作,是指針復(fù)制衫冻,mutableCopy 操作時內(nèi)容復(fù)制诀紊;對 mutable 對象進行 copy 和 mutableCopy 都是內(nèi)容復(fù)制。用代碼簡單表示如下:
[immutableObject copy] // 淺復(fù)制
[immutableObject mutableCopy] //深復(fù)制
[mutableObject copy] //深復(fù)制
[mutableObject mutableCopy] //深復(fù)制