I'm trying to write a simple quiz app. Upon a user tapping a button, I want the action to compare the Button's label to see if it matches the variable of "correctChoice"
I tried the following if/then statement but it doesn't seem to work, as I'm getting the following error "Left side of mutating operator isn't mutable: 'self' is immutable"
//Variables Used
var model = Question.all()
var questionNumber: Int = 1
var totalQuestions: Int = 100
var timeLeft: Int = 60
var currentScore: Int = 0
//Button Code
Button(action: {
if Question.all()[0].choice1 == Question.all()[0].correctChoice {
currentScore += 10
} else {
currentScore += 0
}
}) {
Text(Question.all()[0].choice1)
.font(.system(size: 20))
}
ObservableObjectclasses of typeQuestionandPlayer. (2) You can expose this as either anEnvironmentObjectorObservedObject. (3) Properly done it maintains state. Conclusion? To "interact" between SwiftUI and your "variable" - which is not a UI element but instead something stored - you need to make it "mutable" to SwiftUI by making it@State.