LE SANG
LE SANG

Reputation: 11005

Can't check when NSDate is NULL

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

Answers (3)

Ali Raza
Ali Raza

Reputation: 2816

Try this

if(yourdate){

//Set date }else{ //Not set date }

Upvotes: 0

user529758
user529758

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

torip3ng
torip3ng

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

Related Questions