Whether I should use @property(nonatomic,copy) or @property(nonatomic,strong) for my (NSString *) attr in An object?

后端 未结 4 572
暗喜
暗喜 2021-02-07 07:10
@interface PaneBean : NSObject

@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *type;
@property(nonatomic,assign) NSInteger width;
@end
         


        
4条回答
  •  囚心锁ツ
    2021-02-07 07:48

    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.In non ARC strong will work like retain

    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 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.)

提交回复
热议问题