Use Copy Property for NSString

郭远
2023-12-01

http://fann.im/blog/2012/09/10/use-copy-property-for-nsstring/


一个简短例子来说明一下为什么 NSString @property 最好用 copy 而不是 retain:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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.

参考 NSString property: copy or retain?

 2012-09-10  Dev


 类似资料:

相关阅读

相关文章

相关问答