如何在Core Data中编写BOOL谓词?

发布于 2024-11-10 02:06:12 字数 462 浏览 5 评论 0原文

我有一个 BOOL 类型的属性,并且我想搜索该属性为 YES 的所有托管对象。

对于字符串属性来说,这很简单。我创建了一个像这样的谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName = %@", userName];

但是,如果我有一个名为 selected 的 bool 属性并且我想为此创建一个谓词,我该怎么做呢?我可以做这样的事情吗?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = %@", yesNumber];

或者我是否需要其他格式说明符并只需传递 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];

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

葬﹪忆之殇 2024-11-17 02:06:12

来自 谓词编程指南

您可以指定并测试布尔值是否相等,如以下示例所示:

NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@", [NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == 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.

指尖上得阳光 2024-11-17 02:06:12

斯威夫特4.0

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

Swift 4.0

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))
初见你 2024-11-17 02:06:12

不要转换为 NSNumber,也不要使用双“==”

更适合 Swift >= 4:

NSPredicate(format: "boolAttribute = %d", true)

注意:本例中的“true”是一个 Bool(a结构)

Don't convert to NSNumber, nor use double "=="

More appropriate for Swift >= 4:

NSPredicate(format: "boolAttribute = %d", true)

Note: "true" in this example is a Bool (a Struct)

只涨不跌 2024-11-17 02:06:12

Swift 3

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

在 Swift 3 中,您应该使用 NSNumber(value: true)

不鼓励使用 NSNumber(booleanLiteral: true) 以及一般情况下直接使用任何文字初始化程序,例如 SwiftLint (v. 0.16.1) 将直接生成使用 ExpressibleBy...Literal 初始化程序的警告:

编译器协议初始化违规:不应直接调用在编译器协议(例如 ExpressibleByArrayLiteral)中声明的初始化程序。 (编译器协议初始化)

Swift 3

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

In Swift 3 you should use NSNumber(value: true).

Using NSNumber(booleanLiteral: true) and in general any literal initialiser directly is discouraged and for example SwiftLint (v. 0.16.1) will generate warning for usage ExpressibleBy...Literal initialiser directly:

Compiler Protocol Init Violation: The initializers declared in compiler protocols such as ExpressibleByArrayLiteral shouldn't be called directly. (compiler_protocol_init)

梦冥 2024-11-17 02:06:12

斯威夫特 4

request.predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

斯威夫特 3

request.predicate = NSPredicate(format: "field = %@", value as CVarArg)

Swift 4

request.predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))

Swift 3

request.predicate = NSPredicate(format: "field = %@", value as CVarArg)
蓝天 2024-11-17 02:06:12

Swift 3.0对此做了一些细微的改变:

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(booleanLiteral: true))

Swift 3.0 has made a slight change to this:

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(booleanLiteral: true))
瘫痪情歌 2024-11-17 02:06:12

如果您不想在格式中硬编码属性名称,您可以使用 #keyPath ,例如:

let samplePredicate = NSPredicate(format: "%K != %d", #keyPath(Foo.attr), true)

if you do not want to hard-code the attribute name inside the format you can use #keyPath with something like,

let samplePredicate = NSPredicate(format: "%K != %d", #keyPath(Foo.attr), true)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文