Core Data NSPredicate checking for BOOL value

34,595

Solution 1

Based on Apple Document Here, we can use the following two methods to compare Boolean:

NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@",[NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];

However, the above predicate cannot get out the ones with empty anAttribute. To deal with an empty attribute, you need the following method according to Apple document here:

predicate = [NSPredicate predicateWithFormat:@"firstName = nil"]; // it's in the document

or

predicate = [NSPredicate predicateWithFormat:@"firstName == nil"]; // == and = are interchangeable here

Solution 2

Sneaking in with the Swift 3/4 answer:

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

We have to use NSNumber apparently because a literal bool is not acceptable per Apple.

Stolen from here ;)

Solution 3

For some reason, Flow's solution would not work for me:

NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];

However, this did:

NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == 1"];

Solution 4

I'm late to the party and as discussed using 0 and 1's is the way to go however there is a better way to show this by using NSNumber BOOL literals like @YES or @NO. It converts it to a 1 or a 0 but is more visually friendly.

NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == %@", @NO];

Solution 5

Core Data entities does not have any default values when you create attributes for Entity, so to make your predicate work you should either set default values for boolean attributes or use predicate in this way.

If you provide default value (NO or YES) for any boolean property of Entity then use predicate like this

[NSPredicate predicateWithFormat:@"boolAttribute == %@", @NO];
[NSPredicate predicateWithFormat:@"boolAttribute == NO", @NO];
[NSPredicate predicateWithFormat:@"boolAttribute == %0"];

If you do not have default values or some of Entities was already created without default values then to filter by false value use this sentence:

[NSPredicate predicateWithFormat:@"boolAttribute == %@ || boolAttribute == nil", @NO];
Share:
34,595
user281300
Author by

user281300

Updated on July 09, 2022

Comments

  • user281300
    user281300 almost 2 years

    I am currently having an issue pulling all data from db whereby i.e 1 parameter is TRUE.

    I am using NSPredicate and below is a sample code

    NSManagedObjectContext *context = managedObjectContext_;
    
    if (!context) {
        // Handle the error.
        NSLog(@"ERROR CONTEXT IS NIL");
    }
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"tblcontent" inManagedObjectContext:managedObjectContext_];
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bookmarked == YES"];
    
    [request setPredicate:predicate];
    

    I tried setting predicatewithformat to almost everything but it still does not pull out bookmarks which have a YES value.

    I even tried (@"bookmarked == %d",YES) but with not luck. I don't want to have to get the whole array and then filter it manually by doing if(object.bookmarked == YES) .....blabla.

    I will really appreciate some help.

    Many thanks.

  • user281300
    user281300 over 13 years
    Hi I haven't included that in the code. The results am getting is basically the whole lot. It is as if no filtering is working.
  • user281300
    user281300 over 13 years
    Hi again I think there is something wrong before that. I.e every single content coming back has object.bookmarked = YES even though I have set it to NO by default. I will need to check it out. I reckon , the predicate is working. Thanks anyways.
  • user281300
    user281300 over 13 years
    Hah it is working fine now. stupid me forgot to reset the db from the simulator first doh!! Anyways the code above is fine.
  • Hussain KMR Behestee
    Hussain KMR Behestee about 9 years
    Also for my case Flow's solution didn't work. It worked with anAttribute == 1
  • lewis
    lewis almost 4 years
    The null check is important, especially as once the fetch is done it can appear that bool entries are false, when in fact they are null
  • randomcontrol
    randomcontrol almost 3 years
    OMG! You saved my life... WHY is it working with false but not with true ... It took me a while to even find the line that makes my app crash, because the stack trace was not helpful at all.