Reputation: 11005
I create an entity named "NoteEntity" in CoreData and generate NSManageObject subclass
@interface NoteEntity : NSManagedObject
@property (nonatomic, retain) NSDate * time;
@end
Then I addObserver to check time change when select object:
[self addObserver:self forKeyPath:@"noteEntity.time" options:NSKeyValueObservingOptionOld context:KNoteEntityTime];
Observer Code:
- (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];
}
}
I need to check if noteEntity.time change from NULL but the if (oldTime != NULL) or if(oldTime) not WORK, here is the log
CHANGE:From <null> - (null)
CHANGE:From <null> - 2013-07-29 10:15:58 +0000
Please help me find out what I'm doing wrong. Thanks!
Upvotes: 2
Views: 5810
Reputation:
<null>
is the description of NSNull
, not that of nil
(or Nil
or NULL
which are the same). Try
if (![oldTime isEqual:[NSNull null]])
instead.
Upvotes: 8
Reputation: 585
Try
if (![oldTime isEqual:[NSNull null]]) { ... }
Objects in the containers (NSArray, NSDictionary etc) can not be nil or null in objective c
Upvotes: 0