用copy比用retain安全,当是NSString的时候,其实用copy和retain都行,当用NSMutableString,那么就要用copy,使得NSMutableString的值不会被修改,而用retain的时候,NSMutableString的值会被修改。
举个例子:
- @interface person : NSObject<NSCopying>
- @property (nonatomic,copy)NSString * name;
- @end
复制代码- person * p = [[person alloc]init];
-
- NSMutableString * name = [[NSMutableString alloc]initWithString:@"hello"];
- p.name = name;
-
- [name appendString:@" word"];
- NSLog(@"%@",p.name);
复制代码
可以发现打印结果还是hello;
如果你的name属性是用retain修饰,则打印的是hello word; |