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

后端 未结 4 566
暗喜
暗喜 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:41

    copy sends the copy message the object you set, while strong only retains it (increments the reference count).

    For NSString , or in general any inmutable class with known mutable subclasses(NSArray, NSDictionaty, NSSet), copy is preffered to avoid clients setting a mutable instance and modifying it out of the object.

    For primitive types(int for example) copy/strong does not make sense and by default assign is used. Is up to you if you want to put it explicitly or not.

提交回复
热议问题