Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up a NSPredicate to look for objects that have a nil attribute

I have a ManagedObject class, and one of the members of the class is a NSDate. I would like to display all objects of the class for which the date is NOT set. I tried using a predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(date = NIL)"]; 

But I still get objects where the date is set. What is the right way to set up a predicate for this?

like image 567
mezulu Avatar asked Apr 21 '11 20:04

mezulu


2 Answers

I think it's a case sensitivity issue. You can use "nil" or "NULL", but not "NIL". This works fine for me:

NSPredicate *eventWithNoEndDate = [NSPredicate predicateWithFormat:@"endDate = nil"]; 
like image 68
Christopher Pickslay Avatar answered Nov 09 '22 18:11

Christopher Pickslay


Figured it out. Couldn't do it by using a predicate with a string format, so tried a predicate with a template and it worked. Here's the code that gave me objects that had endDate set to NULL:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"endDate = $DATE"]; predicate = [predicate predicateWithSubstitutionVariables:                    [NSDictionary  dictionaryWithObject:[NSNull null] forKey: @"DATE"]]; 
like image 40
mezulu Avatar answered Nov 09 '22 18:11

mezulu