我在 CoreData 中创建了一个名为“NoteEntity”的实体并生成 NSManageObject 子类
@interface NoteEntity : NSManagedObject
@property (nonatomic, retain) NSDate * time;
@end
然后我添加观察者来检查选择对象时的时间变化:
[self addObserver:self forKeyPath:@"noteEntity.time" options:NSKeyValueObservingOptionOld context:KNoteEntityTime];
观察者代码:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ( context == KNoteEntityTime ) {
NSDate *oldTime = (NSDate *)[change objectForKey:NSKeyValueChangeOldKey] ;
if (oldTime != NULL) {
NSLog(@"CHANGE:From %@ - %@",oldTime,self.noteEntity.time);
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
我需要检查 noteEntity.time 是否从 NULL 更改但 if (oldTime != NULL) 或 if(oldTime) 不起作用,这是日志
CHANGE:From <null> - (null)
CHANGE:From <null> - 2013-07-29 10:15:58 +0000
请帮我找出我做错了什么。谢谢!