【发布时间】:2022-11-25 16:24:34
【问题描述】:
我试图在 swiftui 中使用 switch 语句中的参数运行一个函数,但一直收到“Type '()' cannot conform to 'View'”错误。我认为switch语句和函数应该是正确的。无论我如何处理 case 语句,我仍然会收到相同的错误消息。
struct questionsData: Codable {
enum CodingKeys: CodingKey {
case question
case answers
case correctAnswerIndex
}
//var id = UUID()
var question: String
var answers = [String]()
var correctAnswerIndex: Int
}
struct ThemeView: View {
var quizzes = [questionsData]()
let themeName: String
var body: some View {
let themeselected: String = themeName
var jsonfile: String = ""
switch themeselected {
case "Money Accepted":
jsonfile = "Accounts"
return loadQuizData(jsonname: jsonfile)
case "Computers":
jsonfile = "Computers"
return loadQuizData(jsonname: jsonfile)
default:
Text("invalid")
}
}
func loadQuizData(jsonname: String){
guard let url = Bundle.main.url(forResource: jsonname, withExtension: "json")
else {
print("Json file not found")
return
}
let data = try? Data(contentsOf: url)
var quizzes = try? JSONDecoder().decode([questionsData].self, from: data!)
quizzes = quizzes!
}
}
struct ContentView: View {
@State private var selection: String?
let quizList = ["Money Accepted","Computers","Making an appointment", "Late again", "Shopping", "Renting a place", "Accounts", "Letter Writing", "Planning a business", "Business Expression 1", "Business Expression 2", "How to ask the way"]
var body: some View {
NavigationView{
List(quizList, id:\.self) { quizList in
NavigationLink(destination: ThemeView(themeName: quizList)){
Text(quizList)
}
}
.navigationTitle("Select quiz theme")
}
}
}
请协助......对swiftui还是陌生的。 非常感激。
【问题讨论】:
-
正文用于查看列表、文本、按钮等内容。到目前为止你的身体几乎没有尝试Apple SwiftUI Tutorials你的开关需要在每种情况下返回一个视图而不是调用返回一个空值的函数。你可以在出现时这样做。
-
谢谢你。仍在从 UIKit 迁移的过渡中。将再次运行教程。
标签: swiftui swiftui-list