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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6169121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:08:42  来源:igfitidea点击:

How to write a BOOL predicate in Core Data?

iphoneiosipadcore-datapredicate

提问by Proud Member

I have an attribute of type BOOLand I want to perform a search for all managed objects where this attribute is YES.

我有一个 type 属性,BOOL我想搜索此属性所在的所有托管对象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 selectedand I want to make a predicate for this? Could I just do something like this?

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

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

Or do I need other format specifiers and just pass YES?

或者我是否需要其他格式说明符并通过YES

回答by albertamg

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.

您还可以查看Predicate Format String Syntax

回答by Viktor Kucera

Swift 4.0

斯威夫特 4.0

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

回答by alegelos

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

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

More appropriate for Swift >= 4:

更适合 Swift >= 4:

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

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

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

回答by Lukas Kukacka

Swift 3

斯威夫特 3

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


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

在 Swift 3 中,您应该使用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...Literalinitialiser directly:

NSNumber(booleanLiteral: true)通常不鼓励直接使用任何文字初始化器,例如SwiftLint(v. 0.16.1) 将ExpressibleBy...Literal直接为使用初始化器生成警告:

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

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

回答by SoftDesigner

Swift 4

斯威夫特 4

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

Swift 3

斯威夫特 3

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

回答by Mario Hendricks

Swift 3.0has made a slight change to this:

Swift 3.0对此略有改动:

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