I have an attribute of type BOOL
and I want to perform a search for all managed objects where this attribute is YES
.
For string attributes it is straightforward. I create a predicate like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName = %@", userName];
But how do I do this, if I have a bool attribute called selected and I want to make a predicate for this? Could I just do something like this?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = %@", yesNumber];
Or do I need other format specifiers and just pass YES
?
From Predicate Programming Guide:
You specify and test for equality of Boolean values as illustrated in the following examples:
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@", [NSNumber numberWithBool:aBool]]; NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];
You can also check out the Predicate Format String Syntax.
Swift 4.0
let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With