2

我正在 Xcode 12 beta WatchOS 7 上开发苹果手表应用程序。

我也打算支持 watchOS 6。

以下代码有错误,我不知道如何处理,它属于 SwiftUI 问题:

struct CompactStockListView: View {
  var body: some View {
    NavigationView {
      List(getStocks(), id: \.id) { stock in
        CompactStockRowView(stock: stock)
          .padding(.vertical, 6)
      }
      .navigationBarTitle(Text("Landmarks"))
    }
  }
}

Xcode 注意到错误:

“NavigationView”仅在 watchOS 7.0 或更高版本的应用程序扩展中可用

下一步:尝试此解决方案但未成功:

struct CompactStockListView: View {
  var body: some View {
    getListSafe() // Error
  }
  
  func getListSafe() -> View {  // Error tooo
    if #available(watchOSApplicationExtension 7.0, *) {
      return NavigationView {
        List(getStocks(), id: \.id) { stock in
          CompactStockRowView(stock: stock)
            .padding(.vertical, 6)
        }
        .navigationBarTitle(Text("Landmarks"))
      }
    } else {
      // Fallback on earlier versions
      return List(getStocks(), id: \.id) { stock in
        CompactStockRowView(stock: stock)
          .padding(.vertical, 6)
      }
    }
  }
}
4

1 回答 1

0

尝试以下

  @ViewBuilder
  func getListSafe() -> some View {
    if #available(watchOSApplicationExtension 7.0, *) {
      NavigationView {
        List(getStocks(), id: \.id) { stock in
          CompactStockRowView(stock: stock)
            .padding(.vertical, 6)
        }
        .navigationBarTitle(Text("Landmarks"))
      }
    } else {
      // Fallback on earlier versions
      List(getStocks(), id: \.id) { stock in
        CompactStockRowView(stock: stock)
          .padding(.vertical, 6)
      }
    }
  }
于 2020-07-24T10:50:00.857 回答