arrays - 在 swiftUI 结构中使用数组时出现声明错误
问题描述
此代码适用于 M1 mac 上 BigSur 11.2.3 上的 Xcode 12.5 操场:
import UIKit
var Choice = Array(repeating: "" , count: 3)
Choice [0] = """
This is the land of Gaul
The people are tall
and the children are small
"""
Choice [1] = """
This is the land of Anglos
The people wear black clothes
and they never eat mangoes
"""
Choice [2] = """
This is the land of Hesperia
The people drink their vangueria
and never suffer hysteria
"""
print(Choice [1])
但是,当它在这样的 swiftUI 视图中时:
import SwiftUI
struct ThirdCalcView: View {
@State private var selectedChoice = 0
var str1 = """
This goes
over multiple
lines
"""
var Choice = Array(repeating: "" , count: 3)
Choice [0] = """
This is the land of Gaul
The people are tall
and the children are small
"""
Choice [1] = """
This is the land of Anglos
The people wear black clothes
and they never eat mangoes
"""
Choice [2] = """
This is the land of Hesperia
The people drink their vangueria
and never suffer hysteria
"""
var body: some View {
Form {
Section {
Picker("Choice", selection: $selectedChoice, content: {
Text("France").tag(0)
Text("England").tag(1)
Text("Spain").tag(2)
})
}//section
Section {
Text("The mnemonic is:\(Choice[selectedChoice])")
}//section
} //form
}//some view
} //ThirdCalcView
struct ThirdCalcView_Previews: PreviewProvider {
static var previews: some View {
ThirdCalcView()
}
}
在“var Choice”声明之后,我得到了一系列错误:
我注释了右括号,以确保我没有记错它们。
开头的简单“str1”多行声明很好,所以我认为我的数组声明导致了问题。
我想根据用户的选择显示一个字符串。最终在应用程序中,用户必须在显示响应之前进行三个选择,所以我正在研究使用具有三个维度的数组。
解决方案
您不能在 a orChoice[0] = ...
的顶层编写这样的代码(意思是 )- 它必须在函数或初始化程序内部。struct
class
您有几个选择。最直接的方法就是声明Choice
on 创建,而不是命令式地分配每个索引:
struct ThirdCalcView: View {
@State private var selectedChoice = 0
let Choice = [
"""
This is the land of Gaul
The people are tall
and the children are small
""",
"""
This is the land of Anglos
The people wear black clothes
and they never eat mangoes
""",
"""
This is the land of Hesperia
The people drink their vangueria
and never suffer hysteria
"""]
var body: some View {
//...
}
}
(注意:通常变量名在 Swift 中是小写的)
另一种选择是String
从 switch 语句返回:
struct ThirdCalcView: View {
@State private var selectedChoice = 0
func mnemonic() -> String {
switch selectedChoice {
case 0:
return """
This is the land of Gaul
The people are tall
and the children are small
"""
case 1:
return """
This is the land of Anglos
The people wear black clothes
and they never eat mangoes
"""
case 2:
return """
This is the land of Hesperia
The people drink their vangueria
and never suffer hysteria
"""
default:
assertionFailure("Shouldn't reach here")
return ""
}
}
var body: some View {
Form {
Section {
Picker("Choice", selection: $selectedChoice) {
Text("France").tag(0)
Text("England").tag(1)
Text("Spain").tag(2)
}
}//section
Section {
Text("The mnemonic is:\(mnemonic())")
}//section
} //form
}//some view
} //ThirdCalcView
您可以进一步重构它以根据enum
国家名称进行选择,然后根据枚举值返回不同的助记符。
推荐阅读
- android - 使用 ARCore 在 android 中创建 3d 盒子模型
- ios - 更改视图 Swift ui
- amazon-web-services - FunctionName 的调用容器没有响应
- python - SQL ALCHEMY - 根据父项中指定的条件查询子项
- html - 如何在 HTML 中将图像居中放在另一个图像上
- python - Python 3.7 类属性返回对象
- java - 使用 Hibernate 从数据库中删除实体和关系
- c++ - 有人可以解释第 3 行发生了什么吗?
- java - Maven Build 卡住了很长时间,调试日志显示 [DEBUG] 我们有一个重复的 META-INF/MANIFEST.MF
- android - 回栈和导航