Strong和Copy对于NSString、NSDirectory、NSArray的区别
当NSString、NSDirectory、NSArray在声明属性的时候设置为Strong类型时:
(1) 调用set方法赋值
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSDictionary *dic;
NSMutableString *mustr = [[NSMutableString alloc]initWithFormat:@"123"];
self.str = mustr;
NSLog(@"%@",_str);
[mustr appendString:@"456"];
NSLog(@"%@",_str);
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil];
self.array = muarray;
NSLog(@"%@",_array);
[muarray addObject:@"d"];
NSLog(@"%@",_array);
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"a",@"1",@"b",@"2",nil];
self.dic = muDic;
NSLog(@"%@",_dic);
[muDic setObject:@"3" forKey:@"c"];
NSLog(@"%@",_dic);
输出结果:
(2)直接对实例对象赋值
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSDictionary *dic;
NSMutableString *mustr = [[NSMutableString alloc]initWithFormat:@"123"];
_str = mustr;
NSLog(@"%@",_str);
[mustr appendString:@"456"];
NSLog(@"%@",_str);
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil];
_array = muarray;
NSLog(@"%@",_array);
[muarray addObject:@"d"];
NSLog(@"%@",_array);
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"a",@"1",@"b",@"2",nil];
_dic = muDic;
NSLog(@"%@",_dic);
[muDic setObject:@"3" forKey:@"c"];
NSLog(@"%@",_dic);
输出结果:
结论:当把NSString、NSDirectory、NSArray声明为strong,那么如果原对象的值发生变化,会影响到当前对象的值,不管当前对象是调用点语法赋值还是直接对实例对象赋值。
当NSString、NSDirectory、NSArray在声明属性的时候设置为Copy类型时:
(1)调用set方法赋值
@property (nonatomic, copy) NSString *str;
@property (nonatomic, copy) NSArray *array;
@property (nonatomic, copy) NSDictionary *dic;
self.str = mustr;
NSLog(@"%@",_str);
[mustr appendString:@"456"];
NSLog(@"%@",_str);
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil];
self.array = muarray;
NSLog(@"%@",_array);
[muarray addObject:@"d"];
NSLog(@"%@",_array);
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"a",@"1",@"b",@"2",nil];
self.dic = muDic;
NSLog(@"%@",_dic);
[muDic setObject:@"3" forKey:@"c"];
NSLog(@"%@",_dic);
输出结果:
(2)直接对实例对象赋值
@property (nonatomic, copy) NSString *str;
@property (nonatomic, copy) NSArray *array;
@property (nonatomic, copy) NSDictionary *dic;
NSMutableString *mustr = [[NSMutableString alloc]initWithFormat:@"123"];
_str = mustr;
NSLog(@"%@",_str);
[mustr appendString:@"456"];
NSLog(@"%@",_str);
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil];
_array = muarray;
NSLog(@"%@",_array);
[muarray addObject:@"d"];
NSLog(@"%@",_array);
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"a",@"1",@"b",@"2",nil];
_dic = muDic;
NSLog(@"%@",_dic);
[muDic setObject:@"3" forKey:@"c"];
NSLog(@"%@",_dic);
当设置为Copy属性时,如果调用点语法赋值,当前对象不会因为原对象的改变而改变,但是如果是直接赋值,那么原对象改变那么当前对象也会改变。
总结:当对NSString这种有可变子类的对象直接赋值时,不管声明属性时声明的是copy还是strong,现对象的值都会随着原对象的值而改变。当调用set方法赋值时,如果对象声明的是copy那么现有对象不会因为原对象的改变而改变,如果声明的是strong,那么现有对象会因为原对象的改变而改变。
个人理解:声明为strong时,其实只是把当前对象的指针指向了原有对象的地址,因此当原有对象改变时,现有对象也会改变。直接赋值也应该是这样。当声明为Copy时,其实是把原有对象复制了一份,然后让现有对象的指针指向复制的这一份。