話不多說(shuō)先上代碼
分別copy和strong修飾可變和不可變的字符串
@property (nonatomic,copy) NSString *str_copy;
@property (nonatomic,strong) NSString *str_strong;
@property (nonatomic,strong) NSMutableString *mustr_strong;
@property (nonatomic,copy) NSMutableString *mustr_copy;
//打印方法
-(void)printStrandAdd{
NSLog(@"_str_copy: %p :%@",_str_copy,_str_copy);
NSLog(@"_str_strong: %p :%@",_str_strong,_str_strong);
NSLog(@"_mustr_copy: %p :%@",_mustr_copy,_mustr_copy);
NSLog(@"_mustr_strong: %p :%@",_mustr_strong,_mustr_strong);
}
以可變字符串為源
-(void)MutabStr{
NSMutableString *string = [[NSMutableString alloc]initWithString:@"123456789000"];
self.str_copy = string;
self.str_strong = string;
self.mustr_strong = string;
self.mustr_copy = string;
// [mustr_copy appendString:@"qwertyuiosdfghj"] 會(huì)崩潰
NSLog(@"string源: %p :%@",string,string);
[self printStrandAdd];
NSLog((@"____________修改源________________"));
[string appendString:@"qwertyuiosdfghj"];
NSLog(@"string源: %p :%@",string,string);
[self printStrandAdd];
}
運(yùn)行結(jié)果
2020-01-09 15:26:18.052008+0800 testCI[7652:4203196] ==========Mutab==============
2020-01-09 15:26:18.052140+0800 testCI[7652:4203196] string源: 0x28181bdb0 :123456789000
2020-01-09 15:26:18.052628+0800 testCI[7652:4203196] _str_copy: 0x281658940 :123456789000
2020-01-09 15:26:18.052684+0800 testCI[7652:4203196] _str_strong: 0x28181bdb0 :123456789000
2020-01-09 15:26:18.052730+0800 testCI[7652:4203196] _mustr_copy: 0x281658760 :123456789000
2020-01-09 15:26:18.052761+0800 testCI[7652:4203196] _mustr_strong: 0x28181bdb0 :123456789000
2020-01-09 15:26:18.052858+0800 testCI[7652:4203196] ____________修改源________________
2020-01-09 15:26:18.053029+0800 testCI[7652:4203196] string源: 0x28181bdb0 :123456789000qwertyuiosdfghj
2020-01-09 15:26:18.053294+0800 testCI[7652:4203196] _str_copy: 0x281658940 :123456789000
2020-01-09 15:26:18.053331+0800 testCI[7652:4203196] _str_strong: 0x28181bdb0 :123456789000qwertyuiosdfghj
2020-01-09 15:26:18.053365+0800 testCI[7652:4203196] _mustr_copy: 0x281658760 :123456789000
2020-01-09 15:26:18.053610+0800 testCI[7652:4203196] _mustr_strong: 0x28181bdb0 :123456789000qwertyuiosdfghj
可見(jiàn)對(duì)于可變的源字符串,修改內(nèi)容時(shí)候积锅,指針地址不會(huì)改變?yōu)樵刂贰tr_copy萤彩、self.mustr_copy 賦值之后重新生成了一塊地址,是深復(fù)制端朵。經(jīng)過(guò)copy修飾過(guò)的可變字符串焰雕,變?yōu)椴豢勺儯羰荹mustr_copy appendString:@"qwertyuiosdfghj"] 會(huì)崩潰
以不可變字符串為源
-(void)stringNomauta{
NSString *string = [NSString stringWithFormat:@"123456789000"];
self.str_copy = string;
self.str_strong = string;
self.mustr_strong = string;
self.mustr_copy = string;
NSLog(@"string源: %p :%@",string,string);
[self printStrandAdd];
NSLog(@"____________修改源________________");
string = [NSString stringWithFormat:@"qwertyuiosdfghj"];
NSLog(@"string源: %p :%@",string,string);
[self printStrandAdd];
}
運(yùn)行結(jié)果
2020-01-09 15:26:18.048870+0800 testCI[7652:4203196] ==========NoMutab==============
2020-01-09 15:26:18.049020+0800 testCI[7652:4203196] string源: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049061+0800 testCI[7652:4203196] _str_copy: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049101+0800 testCI[7652:4203196] _str_strong: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049135+0800 testCI[7652:4203196] _mustr_copy: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049168+0800 testCI[7652:4203196] _mustr_strong: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049204+0800 testCI[7652:4203196] ____________修改源________________
2020-01-09 15:26:18.049294+0800 testCI[7652:4203196] string源: 0x28187a0a0 :qwertyuiosdfghj
2020-01-09 15:26:18.049496+0800 testCI[7652:4203196] _str_copy: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049646+0800 testCI[7652:4203196] _str_strong: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.049775+0800 testCI[7652:4203196] _mustr_copy: 0x28164ffe0 :123456789000
2020-01-09 15:26:18.051861+0800 testCI[7652:4203196] _mustr_strong: 0x28164ffe0 :123456789000
可見(jiàn)對(duì)于不可變的源字符串客峭,無(wú)論copy還是strong豫领,都是淺復(fù)制,僅復(fù)制指針舔琅。NSString 修改內(nèi)容時(shí)等恐,會(huì)開(kāi)辟一塊新的內(nèi)存,有新的指針。而愿地址被 str_copy 课蔬、self.str_strong囱稽、 self.mustr_strong 、self.mustr_copy 指向原地址不會(huì)改變二跋。
拓展
不管是集合類對(duì)象(NSArray战惊、NSDictionary、NSSet ... 之類的對(duì)象)扎即,還是非集合類對(duì)象(NSString, NSNumber ... 之類的對(duì)象)吞获,接收到copy和mutableCopy消息時(shí),都遵循以下準(zhǔn)則:
- 1.copy 返回的是不可變對(duì)象(immutableObject)谚鄙;如果用copy返回值調(diào)用mutable對(duì)象的方法就會(huì)crash各拷。
- 2.mutableCopy 返回的是可變對(duì)象(mutableObject)