2

我正在使用 Core Data,并且想在设置属性时运行一些自定义代码。

@interface FTRecord : NSManagedObject

@property (nonatomic) NSTimeInterval timestamp;


@implementation FTRecord

@dynamic timestamp;

-(void)setTimestamp:(NSTimeInterval)newTimestamp
{
    //run custom code....

    //and now how to pass the value to the actual property?
    [self setTimestamp:newTimestamp];
}

在这种情况下,我为时间戳属性定义了设置器主体。但是如何设置属性的值而不遇到递归循环呢?

4

1 回答 1

2

为每个属性生成了一个神奇的访问器,在您的情况下setPrimitiveTimestamp:,您可以使用它来调用它。查看 NSManagedObject 的文档- (void)setPrimitiveValue:(id)value forKey:(NSString *)key

所以你要:

-(void)setTimestamp:(NSTimeInterval)newTimestamp
{
    //run custom code....

    //and now how to pass the value to the actual property?
    [self willChangeValueForKey:@"timestamp"];
    [self setPrimitiveTimestamp:newTimestamp];
    [self didChangeValueForKey:@"timestamp"];
}
于 2013-10-30T17:28:12.273 回答