90

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?

Souljacker
  • 784
  • 13
  • 34
Proud Member
  • 38,700
  • 43
  • 143
  • 225

6 Answers6

105

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.

albertamg
  • 28,172
  • 6
  • 61
  • 70
  • @inorganik I've read a similar claim [here](http://stackoverflow.com/a/22043435/375300). I don't know what is the reason for this but afaik it *should* work and it does for me. – albertamg May 05 '15 at 14:40
  • @inorganik I also tried it and it worked ("it does for me"). I don't know what you tried. I'm curious about it. If you want, share a link to a test case so I can try the exact same code. – albertamg May 05 '15 at 21:22
  • I retested and it worked... so I was wrong. Something else must have caused my case to fail. Either way +1 on your answer – inorganik May 05 '15 at 21:27
  • @inorganik oh I see. If you find out what caused your case to fail i would love to know. Thanks for the +1! :) – albertamg May 05 '15 at 21:31
96

Swift 4.0

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(value: true))
Viktor Kucera
  • 5,779
  • 2
  • 30
  • 38
  • Is there any way to fetch all objects for bool is equal to nil ?? Or does it always have to be either true or false ?? – Apoorv Mote Jul 17 '16 at 18:07
  • @ApoorvMote, as of Xcode 9.4, Swift 4, yes: if you use a regular Bool value in the predicate, items with `nil` attribute values will be found. For example, set an attribute's value to `nil` (we'll say you called it `boolAttribute` as shown in this answer). Then, create your predicate: `let predicate = NSPredicate(format: "boolAttribute == %@", boolValue)`. *Note: `boolValue` should equal `true` or `false`, NOT `NSNumber(value: boolValue)`.* The resulting predicate will be: `boolAttribute == nil`. That predicate *will* return any objects where `boolAttribute` is `nil`. Might not work forever... – leanne Jul 10 '18 at 20:10
  • This is works. You can simply use "boolAttribute == 1" format also. – Zoltan Vinkler Nov 20 '18 at 21:14
44

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)

mahan
  • 4,950
  • 1
  • 12
  • 42
alegelos
  • 1,553
  • 11
  • 19
18

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)

Lukas Kukacka
  • 7,404
  • 2
  • 21
  • 47
3

Swift 4

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

Swift 3

request.predicate = NSPredicate(format: "field = %@", value as CVarArg)
SoftDesigner
  • 5,089
  • 2
  • 45
  • 45
  • 2
    This causes a crash with Xcode 9.3 and Swift 4.1. Use [this method](https://stackoverflow.com/a/34631602/3151675) instead. – Tamás Sengel Apr 04 '18 at 14:33
2

Swift 3.0 has made a slight change to this:

let predicate = NSPredicate(format: "boolAttribute == %@", NSNumber(booleanLiteral: true))
Mario Hendricks
  • 607
  • 8
  • 6