swift - 不能对“Bool”类型的非可选值使用可选链接
问题描述
为什么我不能使用c?swiftui 中的 1 : 2 语法?有什么解决办法吗?
import SwiftUI
struct ContentView: View {
var c: Bool = false
var body: some View {
Text("Hello World")
.foregroundColor(c? .red : .blue)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
要正确使用三元运算,您需要在 thec
和 the之间放置一个空格?
.foregroundColor(c ? .red : .blue)
你可以在这里看到更多,在三元条件运算符下的 Swift 文档中
推荐阅读
- php - PHPMailer 不起作用但没有错误,smtpdebug 设置为 2
- kendo-ui - 如何使用 excel 下载功能制作指向 Kendo Grid 内部的锚链接?
- c++ - Crisis Nanosuit 中的多个纹理 - 使用 Assimp 加载模型
- java - Could not get resource firebase-measurement-connector-impl-17.0.5-javadoc.jar
- python - 如何在 Python 中修复 SQLite 中的语法错误
- javascript - 如何使用 jQuery 从网页中删除复制内容的内联样式?
- xamarin.forms - API 27 中未显示本地通知
- android - 从另一个 Fragment 调用元素
- sql-server - MS SQL 作业目标服务器问题
- java - Maven 阴影插件 - 无法执行目标