我在CoreData中创建一个名为“NoteEntity”的实体,并生成NSManageObject子类
@interface NoteEntity : NSManagedObject
@property (nonatomic, retain) NSDate * time;
@end
然后我选择addObserver来检查选择对象时的时间变化:
[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)不是WORK,这里是日志
CHANGE:From <null> - (null)
CHANGE:From <null> - 2013-07-29 10:15:58 +0000
请帮我看看我做错了什么。谢谢!
答案 0 :(得分:7)
<null>
是NSNull
的说明,而不是nil
(或Nil
或NULL
相同的说明)。尝试
if (![oldTime isEqual:[NSNull null]])
代替。
答案 1 :(得分:0)
尝试
if (![oldTime isEqual:[NSNull null]]) { ... }
容器中的对象(NSArray,NSDictionary等)在目标c中不能为零或为空
答案 2 :(得分:0)
试试这个
if(yourdate){
//设定日期 }其他{ //没有设定日期 }