copy特性,傳入(賦值)不可變對(duì)象時(shí)是淺拷貝:
例如,當(dāng)name屬性為copy特質(zhì)時(shí),str_1賦值操作時(shí)淺拷貝

test copy

對(duì)象地址沒有變
傳入可變字符串時(shí)是深拷貝:
##用在可變字符串時(shí)是深拷貝:##
//屬性是iOS管理變量數(shù)據(jù)存取的方法
@property (nonatomic, strong) NSString *name;
@property (nonatomic, copy) NSString *meng;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self testCopyAndStrong];
//[self testString];
}
-(void)testCopyAndStrong {
NSMutableString *str = [[NSMutableString alloc] initWithString:@"meng"];
self.meng = str;
self.name = str;
[str appendString:@"zhiqi"];
//實(shí)際上不可變字符串能拼接是因?yàn)樗皇怯迷撟址母北緛砥唇? NSString *str1 = [str1 stringByAppendingString:@"mmm"];
//&打印指針的地址,沒有&則是打印指針?biāo)赶驅(qū)ο蟮牡刂? NSLog(@"%p, %p", str, &str);
//strong特性,指針地址不同,但是指針?biāo)赶驅(qū)ο蟮牡刂废嗤?,是淺拷貝(地址拷貝)
NSLog(@"%p, %p", self.name, &_name);
//copy特性,指針地址不同,指針?biāo)赶虻膶?duì)象的地址也不同,是深拷貝(內(nèi)容拷貝)
NSLog(@"%p, %p", self.meng, &_meng);
NSLog(@"%@", str);
//self.name打印出mengzhiqi,可見strong特性只是把指針?biāo)赶驅(qū)ο蟮牡刂房截惲? NSLog(@"%@", self.name);
NSLog(@"%@", self.meng);
}
-(void)testString {
NSString *str = @"meng";
self.meng = str;
self.name = str;
//&打印指針的地址,沒有&則是打印指針?biāo)赶驅(qū)ο蟮牡刂? NSLog(@"%p, %p", str, &str);
NSLog(@"%p, %p", self.name, &_name);
NSLog(@"%p, %p", self.meng, &_meng);
}