First, you do not need / want the question array to be declared as @State. This puts a wrapper around the parameter so that the View can remember the 'state' when the View is updated, or re-displayed. You should just have the array of questions.
Removing the @State will cause two issues. The func 'createAllQuestions()' will have to become a mutating func
. This is turn means that you cannot use the .onAppear
. It will have to be replaced by an init
.
Remove the @State
var allQuestions: [Question] = []
Put this somewhere in your ContentView struct, say just before the var body: some View {
init() {
createAllQuestions()
}
comment out or remove the .onAppear
// .onAppear(perform: createAllQuestions)
Change the func
to a mutating func
mutating func createAllQuestions() {
for firstNum in (1...12) {
for secondNum in 1...12 {
allQuestions.append(Question(firstNum: firstNum, secondNum: secondNum))
}
}
}