我可以在接口中使用readonly装饰核心数据属性,然后在实现中进行读写吗?

时间:2013-04-14 12:12:39

标签: ios core-data

在我的NSManagedObject子类中,我想在接口中将特定属性指定为readonly,但在实现中进行readwrite。我之前和其他课程一样。但是Core Data生成的子类在实现中使用了@dynamic,似乎我无法将读写器装饰器添加到@dynamic,就像可以使用@property一样。

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以将头文件中的属性声明为readonly,然后在实现中为setter提供您自己的实现:

(使用ARC)

@interface Person : NSManagedObject

@property (nonatomic, readonly) NSString * name;

- (void)changeName;

@end

@implementation Person

@dynamic name;

- (void)changeName
{
    self.name = @"Test";
}

- (void)setName:(NSString *)name
{
    [self willChangeValueForKey:@"name"];
    [self setPrimitiveValue:name forKey:@"name"];
    [self didChangeValueForKey:@"name"];
}