to check an coredata object is nil

yjw44 picture yjw44 · Apr 20, 2012 · Viewed 18.1k times · Source

I want to find out the objects in the core data, my code:

Types:

signedDate (Date)

alarmDate(Date)

starTime(NSDate)

endTime(NSDate)

NSString *str = @"(signedDate >= %@) AND (signedDate < %@) AND (alarmDate == nil)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:str, starTime, endTime];
[request setPredicate:predicate];
NSArray *results=[context executeFetchRequest:request error:nil];

predicate is wrong ? How to judge an coredata object is nil? What the predicate should be?

Answer

mprivat picture mprivat · Apr 20, 2012

I don't think the predicate syntax calls for == nil. Use only one =

NSString *str = @"(signedDate >= %@) AND (signedDate < %@) AND (alarmDate = nil)";

Your code above works right. It should be YES since it's nil.

BOOL ok;
predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];
ok = [predicate evaluateWithObject:[NSDictionary dictionary]];

ok = [predicate evaluateWithObject:
[NSDictionary dictionaryWithObject:[NSNull null] forKey:@"firstName"]];