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 *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.)
由此,我们也可以看出copy和retain的区别了。
分享到:
相关推荐
nsstring 详细描述 大多数对于NSString类型的对象作为属性都用的是copy
模型属性*/ @property (nonatomic, copy) NSString * city; @property (nonatomic, copy) NSString * content; @property (nonatomic, copy) NSString * create_time; @property (nonatomic, copy) NSString * ...
@property (nonatomic, copy) NSString *name; @property (nonatomic, copy) Class type; @property (nonatomic, copy) NSString *typeName; @property (nonatomic, assign, readonly) VDPropertyPrimitiveType ...
前言 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择...@property (nonatomic, copy) NSString *myCopyString; - (void)stringTest { NSMutableString *mutableStr = [NSMutableString stringWit
@property (nonatomic,copy) void(^downLoadImageError)(NSError *error,NSString *imageUrl); //imageUrlString或imageName - (instancetype)initWithFrame:(CGRect)frame WithImageNames:(NSArray *)...
@property 属性其实是对成员变量的一种封装。我们先大概这样理解: @property = Ivar + setter + getter ...@property(nonatomic,copy)NSString *name1; @end @implementation Person - (instancetype)ini
iOS单利用法举例 { NSString *_str; } @property(nonatomic,copy)NSString *str;
@property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *lastName; @end ###表操作 //创建表 [CKTestModel createTable]; //创建索引 [CKTestModel createIndex:@"testI
IOS 中runtime使用方法整理 做iOS的朋友都知道或听说runtime,这个东西很像java的反射机制,但功能远胜于java的反射。通过runtime我们可以动态的向一个类中添加属性、成员...@property(nonatomic,copy) NSString *publi
@property ( nonatomic , copy ) NSString *color; @property ( nonatomic , copy ) NSNumber *favoriteNumber; @property ( nonatomic , assign ) NSInteger leastFavoriteNumber; @end ... obj = [[MYObject alloc...
@property (nonatomic,copy)LocationToolData longitudeAndLatitude;//经纬度 +(instancetype)shareLocation; //创建开启定位对象 -(void)setUpLocation; //开启定位 -(void)startUserLocationService; //释放 -...
RestKit RestKit是一个现代的Objective-C框架,用于在iOS和Mac OS X上实现RESTful Web服务客户端。...@property ( nonatomic , copy ) NSString *username; @property ( nonatomic , copy ) NSString *text; @
头部标题 / @property (copy, nonatomic) NSString *headerTitle; /* 头部view / @property (strong, nonatomic) UIView *headerView; /* 头部view的高度 / @property (assign, nonatomic) CGFloat ...
简单的自定义代码块使用方法。 typedef void (^myBlock)(NSInteger i); @property (copy, nonatomic) void (^anothBlock)(NSString *); void (^printBlock)(NSString *str);
@property (nonatomic, copy) NSString *placeholder; /** 占位文字的颜色 */ @property (nonatomic, strong) UIColor *placeholderColor; /** 占位文字的大小 */ @property (nonatomic, strong) UIFont *...
@property ( nonatomic , copy , readonly ) NSString *title; @property ( nonatomic , strong , readonly ) NSDate *date; @property ( nonatomic , assign , readonly ) BOOL showsAlert; @end 仅需三个简单的...
XUserDefaults使用NSUserDefault的类别,用runtime将key与属性的get set方法...如下:@interface NSUserDefaults (Utils)@property (nonatomic, copy) NSString *name;@endc、 使用时 import 分类头文件:#import "NSUs
文本视图KTTextView 派生自 UITextView,并通过新功能对其... @property (nonatomic, copy) NSString *placeholderText; 占位符颜色占位符文本的颜色。 默认值为浅灰色。 @property (nonatomic, retain) UIColor *place
UITableview控件基本使用 ...@property (nonatomic, copy) NSString *icon; /** * 名称 */ @property (nonatomic, copy) NSString *name; /** * 描述 */ @property (nonatomic, copy) NSString *intro;
ZPCusCount//textfeild 是否允许手动输入(默认NO)@property (nonatomic,assign) BOOL is...//设置textfeild的值@property (nonatomic,copy) NSString *textCount;//设置textfeild的最大值@property (nonatomic,co