使用 SwiftUI 运行 Xcode 11 时出现 Swift 编译器问题

标签 swift swiftui

<分区>

我正在尝试运行我的 SwiftUI 演示,但我遇到了奇怪的编译器问题:

'Color' is not convertible to 'Color?'

Unable to infer complex closure return type; add explicit type to disambiguate

struct LandmarkRow: View {
  var landmark: Landmark
    
  var body: some View {
    HStack {
      landmark.image(forSize: 50)
      Text(landmark.name)
      Spacer()
    
      if landmark.isFavorite {
        Image(systemName: "star.fill")
           .imageScale(.medium)
           .foregroundColor(.yellow) // Here 'Color' is not convertible to 'Color?'
      }
    }
  }
}

struct LandmarkList: View {
  @State var showFavoritesOnly = true
    
  var body: some View {
      NavigationView {
          List {
              Toggle(isOn: $showFavoritesOnly) {
                  Text("Favorites only")
              }
    
              ForEach(landmarkData) { landmark in // Here Unable to infer complex closure return type; add explicit type to disambiguate
                  if !self.showFavoritesOnly || landmark.isFavorite {
                      NavigationButton(destination: LandmarkDetail(landmark: landmark)) {
                          LandmarkRow(landmark: landmark)
                      }
                  }
              }
              .navigationBarTitle(Text("Landmarks"))
          }
      }
   }
}

最佳答案

嗯...这与 Landmark 文件中缺少的 isFavorite 属性有关。 (不见了🤦​​‍♂️)

所以我要填补一个关于这个错误的编译器消息的错误。这绝对是一个错误。雷达编号FB6118410

如果你想修复它,你需要做两件事: 1

  1. 将其添加到 Landmark 结构 var isFavorite: Bool
  2. 您需要编辑 landmarkData.json 并添加此键和值 "isFavorite": true 否则它会崩溃。

关于使用 SwiftUI 运行 Xcode 11 时出现 Swift 编译器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452691/

相关文章:

ios - Swift-无法使用 SpriteKit 呈现 Game Center

ios - swift 上的自定义标记图像(MapKit)

swift - 带条件的@IBSegueAction

ios - SwiftUI .popover 框架大小不正确

ios - 列和行索引比 SKTileMapNode 中的列和行数更高

ios - 在包含多个图像的UIDocument中处理图像加载/存储

swift - iOS 14 UISplitViewController(侧边栏)与三栏侧边栏切换图标行为

Swift NSTextField,在 Cmd + Enter 上执行

ios - 如何将渐变应用于任何 View 的 accentColor - SwiftUI

SwiftUI 文本动画不透明度不起作用