gpt4 book ai didi

ios - 无法检查 NSDate 何时为 NULL

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:12 25 4
gpt4 key购买 nike

我在 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

请帮我找出我做错了什么。谢谢!

最佳答案

<null>NSNull 的描述, 不是 nil 的那个(或 NilNULL 相同)。尝试

if (![oldTime isEqual:[NSNull null]])

相反。

关于ios - 无法检查 NSDate 何时为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com