原文地址
For attributes whose type is an immutable value class that conformsto theNSCopying
protocol,you almost always should specifycopy
inyour@property
declaration.Specifyingretain
issomething you almost never want in such a situation.
Here's why you want to do that:
NSMutableString *someName = [NSMutableString stringWithString:@"Chris"];
Person *p = [[[Person alloc] init] autorelease];
p.name = someName;
[someName setString:@"Debajit"];
The current value of thePerson.name
propertywill be different depending on whether the property isdeclaredretain
orcopy
—it will be@"Debajit"
ifthe property is markedretain
,but@"Chris"
ifthe property is markedcopy
.
Since in almost all cases you want topreventmutatingan object's attributes behind its back, you should mark theproperties representing themcopy
.(And if you write the setter yourself instead of using@synthesize
youshould remember to actually usecopy
insteadofretain
init.)