问题描述
我有一个名为项核心数据模型对象。
在此我有一个属性IsFavorite。
I have a core data model object called Entry.In this I have an attribute IsFavorite.
我想用一个NS predicate过滤我NSFetchedResultsController的结果。
I would like to use an NSPredicate to filter the results of my NSFetchedResultsController.
目前我得到EXC_BAD_ACCESS当抓取会执行。
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 *aFetchedResultsController = [[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();
}
如果我删除,设置在fetchRequest的predicate行了,我的code完全执行
IF I remove the line that sets the predicate on the fetchRequest, my code executes perfectly.
我清楚地n00bin出来的predicate,但有很多麻烦试图找出如何在从核心数据模型对象一个布尔值执行操作。值得注意的是,有关于如何用细绳int值做到这一点的答案,但我不能找到一个Boolean实例。
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.
非常感谢!
推荐答案
这是不是真的具体到NS predicate ......只要你有%@
在格式字符串中,相应的值必须是一个指向对象,BOOL不符合。所以强似YES,通 [NSNumber的numberWithBool:YES]
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]
.
在X $ C $的C更新版本和软件开发工具包比我原来写的答案,你可以使用 @YES
而不是的[的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]
.
这篇关于NS predicate - 根据布尔储值过滤值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!