I am trying to compose a dynamic filter in my Codre Data filters like this (using SwiftUI):
@State var NumberOfRooms: Int
init(NumberOfRooms: Int) {
self.NumberOfRooms = NumberOfRooms
}
@FetchRequest(entity: Listing.entity(),sortDescriptors:[NSSortDescriptor(keyPath: \Listing.publishdate, ascending: false),],predicate: NSPredicate(format: "category == %@ AND rooms ", "House", "\($NumberOfRooms)"))
In the view i want to modify the value of the variable like this:
Text("2")
.onTapGesture {
self.NumberOfRooms = 2
}
I get the error at NSPredicate:
Cannot use instance member '$NumberOfRooms' within property initializer; property initializers run before 'self' is available
Basically i want to dynamically compose the predicates. Any help is deeply appreciated.