【问题标题】:Cannot use optional chaining on non-optional value of type 'Bool' [duplicate]不能对“Bool”类型的非可选值使用可选链接[重复]
【发布时间】:2020-02-03 14:21:28
【问题描述】:

为什么我不能使用 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()
    }
}

【问题讨论】:

  • 不是c?,是c?有空格
  • 真的!谢谢!

标签: swift


【解决方案1】:

要正确使用三元运算,您需要在 c? 之间放置一个空格

.foregroundColor(c ? .red : .blue)

您可以在三元条件运算符下的 Swift 文档中看到更多 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2021-11-04
    相关资源
    最近更新 更多