开发者

NSPredicate - filtering values based on a BOOLEAN stored value

I have a core data model object called Entry. In this I have an attribute IsFavorite.

I would like to use an NSPredicate to filter the results of my NSFetchedResultsController.

Currently I am getting EXC_BAD_ACCESS when the fetch executes.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate. 

NSEntityDescription *thisEntry = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext_];
[fetchRequest setEntity:thisEntry];

NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"Entry.isFavorite == %@", [NSNumber numberWithBool: YES]];

[fetchRequest setPredicate:predicate];


NSFetchedResultsController *aFet开发者_运维问答chedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;


NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
    NSlog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

IF I remove the line that sets the predicate on the fetchRequest, my code executes perfectly.

I am clearly n00bin out on the predicate but have had much trouble trying to find out how to perform operations on a BOOLEAN value from a core data model object. It is noted that there are answers on how to do this with a string or int value but I can't find a BOOLEAN example.

Many thanks !


This isn't really specific to NSPredicate... Whenever you have %@ in a format string, the corresponding value must be a pointer to an object, and BOOL doesn't qualify. So instead of passing YES, pass [NSNumber numberWithBool: YES].


In newer versions of Xcode and the SDKs than when I originally wrote the answer, you can use @YES instead of [NSNumber numberWithBool: YES].


From Apple's documentation:

Boolean Values

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"];

However, something that caught me out:

Be sure to untick the Optional entity attribute and set a Default Value of YES or NO, otherwise the column will be empty (null?) and the above predicates will not match rows which are not explicitly set.

I used the great app sqllitebrowser to diagnose this one, by looking at the resulting simulator database.


If you know you're looking for a YES (and therefore don't need to switch between YES or NO in different situations), this worked for me:

[NSPredicate predicateWithFormat:@"isFavorite == 1"]


For me, on SWIFT 3.0 use NSNumber and %@ didn't work, I had to use integer values:

NSPredicate(format: "yourAttributeName == %i", yourBooleanValue ? 1 : 0)


swift 3 version worked great for me:

 let predicate = NSPredicate(format: "isFriend == %@" ,NSNumber(booleanLiteral: false))

following @scipilot answer the bool shouldn't be an optional. thanks for that!


Interestingly if you "know" which one you want, you can just have:

let p = NSPredicate(format: "showMe == true")

example,

let r = NSFetchRequest<NSFetchRequestResult>(entityName: "CDThing")
let p = NSPredicate(format: "showMe == true")
r.predicate = p

You would do this for things like "subscribed", "new" etc.


on iOS 10, xcode 9.0, looking for a core data managedObject with Boolean NO attribute:

[NSPredicate predicateWithFormat:@"attributeName == nil"]


Check nil OR false

If you didn't set value before , it may come into nil category even if Bool data type

 let predicate = NSPredicate(format: "isDeviceLine == nil OR isDeviceLine == %@", NSNumber(value: false))


For me it worked like this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(isComplete LIKE[c] %@)",[NSString stringWithFormat:@"%@",[NSNumber numberWithBool:YES]]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜