之前都是靠记忆来维持copy和strong 也有问过我 。 我自己说起来才感觉都说不很清楚
通过简单都代码来验证一下 。 有时候花点时间验证一下才能让思路更加清晰
NSMutableString * mustr = [NSMutableString stringWithString:@"good"];
self.str = mustr;
self.fuzhiString = mustr;
NSLog(@"self.str is (%@) ==== str,self.fuzhiString is (%@)",self.str,self.fuzhiString);
NSLog(@"self.str *p is (%@) ==== str,self.fuzhiString *p is (%@)",self.str,self.fuzhiString);
注释:打印结果 == self.str is (good) ==== str,self.fuzhiString is (good)
self.str *p is (0x600000059350) ==== str,self.fuzhiString *p is (0xa000000646f6f674)
[mustr appendString:@"night"];
NSLog(@"self.str is (%@) ==== str,self.fuzhiString is (%@)",self.str,self.fuzhiString);
NSLog(@"self.str *p is (%@) ==== str,self.fuzhiString *p is (%@)",self.str,self.fuzhiString);
注释:打印结果 ==self.str is (goodnight) ==== str,self.fuzhiString is (good)
self.str *p is (0x600000059350) ==== str,self.fuzhiString *p is (0xa000000646f6f674)
打印结果: copy修饰的属性为深copy(开辟一个新内存指向一个新的值)
strong修饰的属性为浅copy(跟着mustr的值的改变而改变);
同时表明字符串为了防止不经意的修改 应该使用copy,养成好的习惯.
当然用NSString用copy和strong是没有什么区别的