Core Data NSPredicate use with dynamic Boolean argument

In my Core Data, I have a "Car" Entity with 3 attributes:

  • id:UUID
  • make:String
  • sold:Bool

in my struct:View I have the following two Sate vars

@State private var make = "Ford"
@State private var carSold = true

This following request correctly returns all of the CarMO objects

@FetchRequest(sortDescriptors: ) var cars: FetchResults

When I try to use the predicate the filter the results on the @State var

The following NSPredicate correctly returns only the cars of make "Ford".

cars.nsPredicate = NSPredicate(format: "make = %@", make)

The following NSPredicate correctly returns only the cars that are sold but is not dynamic.

cars.nsPredicate = NSPredicate(format: "sold = true")

I have tried every possible format I could think of to reference the @State var carSold but they either fail or return all cars"

failure example:

cars.nsPredicate = NSPredicate(format: "sold = %@", carSold)

I have search the web and got nothing applicable to booleans.

I bought "SwiftUI For MasterMinds 2nd Edition 2022". Chapter 13.4 Core Data has a good section on "Predicates" but only string and number as arguments. Nothing on booleans.

I read Apple's "Predicate Programming Guide" and its referenced "Predicate Format String Syntax". The only thing I found was the following:

Boolean Value Predicates

TRUEPREDICATE

A predicate that always evaluates to TRUE

FALSEPREDICATE

A predicate that always evaluates to FALSE

I tried to see how that would be of use in my situation but again came up empty.

I would really appreciate if someone could show me how to reference a boolean in the NSPredicate format string or maybe point me to some documentation that has the answer.

Thank you.

If I understand correctly, try the following:

NSPredicate(“sold = %@“, carSold as NSNumber)

%@ is matching an objective C object. There are numeric format characters but I never remember them so I almost always fallback to this.

Thank you!!!. Your answer worked perfectly and once again, I realize how little I understand about Swift. I'm not quite sure how converting a Bool to a NSNumber and then comparing it to another Bool works, but it does.

Again thank you. You saved a very nice iMac from being thrown out a third story window.

(just kidding, I live on a ground floor) ;)