如何在 Core Data 中编写 BOOL 谓词? [英] How to write a BOOL predicate in Core Data?
本文介绍了如何在 Core Data 中编写 BOOL 谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 BOOL
类型的属性,我想搜索该属性为 YES
的所有托管对象.
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];
但是如果我有一个名为 selected 的 bool 属性并且我想为此做一个谓词,我该怎么做?我可以做这样的事情吗?
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];
或者我是否需要其他格式说明符并只通过YES
?
Or do I need other format specifiers and just pass YES
?
推荐答案
来自 谓词编程指南:
您指定并测试布尔值是否相等,如以下示例所示:
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"];
您还可以查看谓词格式字符串语法.
这篇关于如何在 Core Data 中编写 BOOL 谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文