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.