我尝试解开我的可选属性,但收到此错误消息:
包含控制流语句的闭包不能与函数构建器“ViewBuilder”一起使用
我看不出我的代码有什么问题
HStack {
if let height = profile.height {
TagBox(field: "height", value: String(height))
}
TagBox(field: "nationality", value: profile.nationality)
Spacer()
}.padding(.horizontal)
Run Code Online (Sandbox Code Playgroud)
LuL*_*aGa 10
在这种情况下,有两种使用可选项的方法:
第一个,如果你不希望这个视图在 profile.height 为零时显示:
profile.height.map({ TagBox(field: "height", value: String($0))})
Run Code Online (Sandbox Code Playgroud)
第二个,如果您希望显示此视图,但使用默认值:
TagBox(field: "height", value: String(profile.height ?? 0))
Run Code Online (Sandbox Code Playgroud)
在 a 中使用条件绑定ViewBuilder现在完全没问题:
HStack {
if let height = profile.height { // <- This works now in Xcode 12
TagBox(field: "height", value: String(height))
}
TagBox(field: "nationality", value: profile.nationality)
Spacer()
}.padding(.horizontal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4942 次 |
| 最近记录: |