Strong和Copy對于NSString秘血、NSDirectory、NSArray的區(qū)別
當(dāng)NSString、NSDirectory咒精、NSArray在聲明屬性的時候設(shè)置為Strong類型時:
(1) 調(diào)用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);
輸出結(jié)果:
(2)直接對實(shí)例對象賦值
@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);
輸出結(jié)果:
結(jié)論:當(dāng)把NSString密末、NSDirectory握爷、NSArray聲明為strong,那么如果原對象的值發(fā)生變化严里,會影響到當(dāng)前對象的值,不管當(dāng)前對象是調(diào)用點(diǎn)語法賦值還是直接對實(shí)例對象賦值新啼。
當(dāng)NSString、NSDirectory刹碾、NSArray在聲明屬性的時候設(shè)置為Copy類型時:
(1)調(diào)用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);
輸出結(jié)果:
(2)直接對實(shí)例對象賦值
@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);
當(dāng)設(shè)置為Copy屬性時燥撞,如果調(diào)用點(diǎn)語法賦值,當(dāng)前對象不會因?yàn)樵瓕ο蟮母淖兌淖兠灾模侨绻侵苯淤x值物舒,那么原對象改變那么當(dāng)前對象也會改變。
總結(jié):當(dāng)對NSString這種有可變子類的對象直接賦值時戏锹,不管聲明屬性時聲明的是copy還是strong,現(xiàn)對象的值都會隨著原對象的值而改變冠胯。當(dāng)調(diào)用set方法賦值時,如果對象聲明的是copy那么現(xiàn)有對象不會因?yàn)樵瓕ο蟮母淖兌淖兙坝茫绻暶鞯氖莝trong涵叮,那么現(xiàn)有對象會因?yàn)樵瓕ο蟮母淖兌淖儭?/p>
個人理解:聲明為strong時,其實(shí)只是把當(dāng)前對象的指針指向了原有對象的地址伞插,因此當(dāng)原有對象改變時割粮,現(xiàn)有對象也會改變。直接賦值也應(yīng)該是這樣媚污。當(dāng)聲明為Copy時舀瓢,其實(shí)是把原有對象復(fù)制了一份,然后讓現(xiàn)有對象的指針指向復(fù)制的這一份耗美。