初始化器 `init(:_ rowContent:)` 要求 `Type` 符合 `Identifiable`

Initializer `init(:_rowContent:)` requires that `Type` confirm to `Identifiable`

我正在关注 KMM tutorial 并且能够成功 运行 Android 端的应用程序。现在我想测试 iOS 部分。除了下面的编译错误外,一切似乎都很好。我想这一定是微不足道的事情,但由于我 零经验 iOS/Swift,我正在努力修复它。

我的第一次尝试是让 RocketLaunchRow 扩展 Identifiable,但后来我 运行 陷入了其他问题...非常感谢任何帮助。

xcode 版本: 12.1

完整来源:

import SwiftUI
import shared

func greet() -> String {
    return Greeting().greeting()
}

struct RocketLaunchRow: View {
    var rocketLaunch: RocketLaunch

    var body: some View {
        HStack() {
            VStack(alignment: .leading, spacing: 10.0) {
                Text("Launch name: \(rocketLaunch.missionName)")
                Text(launchText).foregroundColor(launchColor)
                Text("Launch year: \(String(rocketLaunch.launchYear))")
                Text("Launch details: \(rocketLaunch.details ?? "")")
            }
            Spacer()
        }
    }
}

extension RocketLaunchRow {
   private var launchText: String {
       if let isSuccess = rocketLaunch.launchSuccess {
           return isSuccess.boolValue ? "Successful" : "Unsuccessful"
       } else {
           return "No data"
       }
   }

   private var launchColor: Color {
       if let isSuccess = rocketLaunch.launchSuccess {
           return isSuccess.boolValue ? Color.green : Color.red
       } else {
           return Color.gray
       }
   }
}

extension ContentView {
    enum LoadableLaunches {
        case loading
        case result([RocketLaunch])
        case error(String)
    }

    class ViewModel: ObservableObject {
            let sdk: SpaceXSDK
            @Published var launches = LoadableLaunches.loading

            init(sdk: SpaceXSDK) {
                self.sdk = sdk
                self.loadLaunches(forceReload: false)
            }

            func loadLaunches(forceReload: Bool) {
                self.launches = .loading
                    sdk.getLaunches(forceReload: forceReload, completionHandler: { launches, error in
                        if let launches = launches {
                            self.launches = .result(launches)
                        } else {
                            self.launches = .error(error?.localizedDescription ?? "error")
                        }
                    })
            }
        }
}

struct ContentView: View {
  @ObservedObject private(set) var viewModel: ViewModel

    var body: some View {
        NavigationView {
            listView()
            .navigationBarTitle("SpaceX Launches")
            .navigationBarItems(trailing:
                Button("Reload") {
                    self.viewModel.loadLaunches(forceReload: true)
            })
        }
    }

    private func listView() -> AnyView {
        switch viewModel.launches {
        case .loading:
            return AnyView(Text("Loading...").multilineTextAlignment(.center))
        case .result(let launches):
            return AnyView(List(launches) { launch in
                RocketLaunchRow(rocketLaunch: launch)
            })
        case .error(let description):
            return AnyView(Text(description).multilineTextAlignment(.center))
        }
    }
}

using a List or ForEach on primitive types that don’t conform to the Identifiable protocol, such as an array of strings or integers. In this situation, you should use id: .self as the second parameter to your List or ForEach

the above,我们可以看到您需要在发生错误的那一行执行此操作:

return AnyView(List(launches, id: \.self) { launch in

我认为这应该可以消除您的错误。