xcode NSPredicate - 基于 BOOLEAN 存储值过滤值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3445431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSPredicate - filtering values based on a BOOLEAN stored value
提问by Lance
I have a core data model object called Entry. In this I have an attribute IsFavorite.
我有一个名为 Entry 的核心数据模型对象。在这个我有一个属性 IsFavorite。
I would like to use an NSPredicate to filter the results of my NSFetchedResultsController.
我想使用 NSPredicate 来过滤我的 NSFetchedResultsController 的结果。
Currently I am getting EXC_BAD_ACCESS when the fetch executes.
目前我在获取执行时得到 EXC_BAD_ACCESS。
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();
}
IF I remove the line that sets the predicate on the fetchRequest, my code executes perfectly.
如果我删除在 fetchRequest 上设置谓词的行,我的代码将完美执行。
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.
我显然不了解谓词,但在试图找出如何对核心数据模型对象的 BOOLEAN 值执行操作时遇到了很多麻烦。请注意,有关于如何使用字符串或整数值执行此操作的答案,但我找不到 BOOLEAN 示例。
Many thanks !
非常感谢 !
回答by JWWalker
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]
.
这并不是真正特定于 NSPredicate ......每当你有%@
一个格式字符串时,相应的值必须是一个指向对象的指针,而 BOOL 不符合条件。因此,不是通过 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]
.
在比我原来写的答案较新版本的Xcode和软件开发工具包,你可以使用@YES
的替代[NSNumber numberWithBool: YES]
。
回答by scipilot
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.
是务必取消选中的Optional
实体属性和设置Default Value
的YES
或NO
,否则列将是空(NULL?)和上述谓词将不匹配未明确设置的行。
I used the great app sqllitebrowser to diagnose this one, by looking at the resulting simulator database.
通过查看生成的模拟器数据库,我使用了很棒的应用程序 sqllitebrowser 来诊断这个问题。
回答by wrees
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:
如果您知道您正在寻找 YES(因此不需要在不同情况下在 YES 或 NO 之间切换),这对我有用:
[NSPredicate predicateWithFormat:@"isFavorite == 1"]
回答by Rotem Doron
swift 3 version worked great for me:
swift 3 版本对我很有用:
let predicate = NSPredicate(format: "isFriend == %@" ,NSNumber(booleanLiteral: false))
following @scipilot answer the bool shouldn't be an optional. thanks for that!
跟随@scipilot 回答 bool 不应该是可选的。感谢那!
回答by Fattie
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.
您可以为“订阅”、“新”等内容执行此操作。
回答by pierre23
For me, on SWIFT 3.0use NSNumber and %@ didn't work, I had to use integer values:
对我来说,在SWIFT 3.0 上使用 NSNumber 并且 %@ 不起作用,我不得不使用整数值:
NSPredicate(format: "yourAttributeName == %i", yourBooleanValue ? 1 : 0)
回答by Enrico Cupellini
on iOS 10, xcode 9.0, looking for a core data managedObject with Boolean NO attribute:
在 iOS 10、xcode 9.0 上,寻找具有布尔 NO 属性的核心数据 managedObject:
[NSPredicate predicateWithFormat:@"attributeName == nil"]
回答by Surender Rathore
For me it worked like this
对我来说它是这样工作的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(isComplete LIKE[c] %@)",[NSString stringWithFormat:@"%@",[NSNumber numberWithBool:YES]]];