NSString property中应该使用copy

For attributes whose type is an immutable value class that conforms to the NSCopying protocol, you almost always should specify copy in your @property declaration. Specifying retain is something you almost never want in such a situation.

Here's why you want to do that:

NSMutableString   * someName  =   [ NSMutableString  stringWithString :@ "Chris" ];

Person   * =   [[[ Person  alloc ]  init ]  autorelease ];
p
. name  =  someName ;

[ someName setString :@ "Debajit" ];

The current value of the Person.name property will be different depending on whether the property is declared retain or copy — it will be @"Debajit" if the property is marked retain, but@"Chris" if the property is marked copy.

Since in almost all cases you want to prevent mutating an object's attributes behind its back, you should mark the properties representing them copy. (And if you write the setter yourself instead of using@synthesize you should remember to actually use copy instead of retain in it.)

由此,我们也可以看出copy和retain的区别了。

如果NSString存的是NSMutableString时  copy是深copy,返回的是另外一个对象 retainCount=1

NSString 是不可变的NSString时 copy是浅copy,retain +1,但是copy返回的NSString不可修改,retain可修改

FROM http://www.cnblogs.com/scorpiozj/archive/2011/01/25/1944496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值