【发布时间】:2023-01-12 18:17:09
【问题描述】:
struct QuizView: View {
// The question pool.
var questions = [("What is the capital of France?", ["Paris", "London", "New York", "San Francisco"], "Paris"),
("What is the capital of Italy?", ["Rome", "Paris", "Madrid", "Berlin"], "Rome"),
("What is the capital of Spain?", ["Barcelona", "Madrid", "Rome", "Paris"], "Madrid"),
("What is the capital of Germany?", ["Berlin", "Paris", "London", "New York"], "Berlin"),
("What is the capital of the UK?", ["London", "Paris", "Madrid", "New York"], "London"),
]
// The current question.
@State var currentQuestion = 0
// The user's score.
@State var score1 = 0
// The indices of the remaining questions.
@State var remainingQuestions = [Int]()
var body: some View {
VStack {
currentView
}.onAppear {
questions.shuffle() // shuffle the questions array
remainingQuestions = Array(0..<questions.count) // recreate remainingQuestions array
}
}
}
错误信息:
不能对不可变值使用可变成员:'self' 是不可变的
我实际上想编写代码,以便问题会随机播放,但显然这发生了,我也是新手。
【问题讨论】:
-
那么错误是什么?你在哪里挣扎?
-
究竟发生了什么,什么错误代码?
-
嗨@Shlok,欢迎来到 Stack Overflow。请更新您的代码以显示重现该问题的最小示例;理想情况下,有人可以将其粘贴到 Xcode 中并运行的
struct ContentView。请看minimal reproducible example -
不能在不可变值上使用可变成员:'self' 是不可变的,这是错误
-
在哪里?哪一行导致错误?这是一个编译错误,而不是运行时错误。