NSMutableString *someName = [NSMutableString stringWithString:@"Chris"];
Person *p = [[[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
意思是如果你使用retain修饰name的时候,最后一个表达式会改变你之前设置的name的值。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.)