Utilizing Swift UI
, in XCode 15.2
, when changing a variable right into a Swift Knowledge
@Question
attribute with a easy predicate, my software now not compiles and throws the error:
The compiler is unable to type-check this expression in affordable time; attempt breaking apart the expression...
My aim within the question is to get all data with a given string in a ‘drinkType’ area matches beer
. I would like this to get the depend of all data that match this worth in that column. So I can current it within the view
When a person selects a button, it ought to add a brand new report to this mannequin in swift knowledge, recalculate the depend, and replace the UI.
the contents of the contentView
are as such:
import SwiftUI
import SwiftData
struct ContentView: View {
//the setting
@Surroundings(.modelContext) var modelContext
//the predicate
@Question(filter: #Predicate<DrinkModel> {
drinkModel in
drinkModel.drinkType == "beer"
}) var beers: [DrinkModel]
//convert these variables into the format above as soon as sort checking sorted out
@State var wine:Int = 0
@State var photographs:Int = 0
@State var cocktails:Int = 0
@State var drinks:Int = 0
var physique: some View {
ZStack {
Shade.teal.ignoresSafeArea()
VStack {
Textual content("DrinkLess v0.01").padding()
Spacer()
HStack {
VStack {
Button("🍺", motion: { self.recordDrink(drinkType: .Beer)}).buttonStyle(LiquorButtonStyle())
Textual content("(beers.depend) Beers")
}
VStack {
Button("🍷", motion: {self.recordDrink(drinkType: .Wine)}).buttonStyle(LiquorButtonStyle())
Textual content("(wine) Wines")
}
}
HStack {
VStack {
Button("🥃", motion: {self.recordDrink(drinkType: .Shot)}).buttonStyle(LiquorButtonStyle())
Textual content("(photographs) Pictures")
}
VStack {
Button("🍸", motion: {self.recordDrink(drinkType: .Cocktail)}).buttonStyle(LiquorButtonStyle())
Textual content("(cocktails) Cocktails")
}
}
Spacer()
Textual content("Whole Drinks this week").padding()
Textual content(String(beers+wine+photographs+cocktails)).font(.daring(.system(measurement: 38))())
}
.padding()
}
}
personal func recordDrink(drinkType: drinkType) {
let dm = DrinkModel(drinkType: drinkType.rawValue, date: Date())
modelContext.insert(dm)
}
}
Returning the variable to be just like the others removes this error, so it appears seemingly the error comes from the processing of the predicate and/or the question.
if it helps, right here is the mannequin being queried. Its quite simple:
import SwiftData
import Basis
@Mannequin
class DrinkModel {
var drinkType: String
var date: Date
init(drinkType: String, date: Date) {
self.drinkType = drinkType
self.date = date
}
}
And an enum
I take advantage of to map a restricted variety of strings to varieties
//wprkaround since Swift Knowledge has bug not supporting Enum values
enum drinkType: String {
case Beer = "beer"
case Wine = "wine"
case Shot = "shot"
case Cocktail = "cocktail"
}
I would like to have the ability to observe the @Question
variables for modifications, and replace Swift UI when the values do.
What can I do if something to simplify both the question or summary the UI to make this possible?