Use copy property for NSString
source link: https://fann.im/blog/2012/09/10/use-copy-property-for-nsstring/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Use copy property for NSString
Sep 10, 2012
一个简短例子来说明一下为什么 NSString @property 最好用 copy
而不是 retain
:
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, copy) NSString *school;
@end
@implementation Person
@synthesize name, school;
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
Person *p = [[Person alloc] init];
NSMutableString *s1 = [NSMutableString stringWithString:@"fannheyward"];
NSMutableString *s2 = [NSMutableString stringWithString:@"hfut"];
p.name = s1;
p.school = s2;
NSLog(@"%@, %@", p.name, p.school); // fannheyward, hfut
[s1 appendString:@"---Heybot"];
[s2 appendString:@"---Heybot"];
NSLog(@"%@, %@", p.name, p.school); // fannheyward---Heybot, hfut
}
}
简单来说就是 NSString 可以通过 NSMutableString (isa NSString) 来进行修改,如果 @property 是 retain
的话就可以绕过 Person 实例来修改 name 值(因为 name 指向 s1),大部分时候这种情况都是不应该发生的,用 copy
就没有这个问题。
这样来说象 NSArray/NSDictionary 等可修改类型都应该用 copy
。
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.
Was this page helpful?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK