【问题标题】:Referencing initializer 'init(destination:label:)' on 'NavigationLink'在“NavigationLink”上引用初始化程序“init(destination:label:)”
【发布时间】:2020-04-22 13:38:49
【问题描述】:

我正在尝试创建List,但出现如下代码错误。

struct BookView: View {

    var books : [BookModel] = []
    var body: some View {

        NavigationView {
            List(self.books, id: \.self) { book in
                NavigationLink(destination: Text(book.title ?? "Unknown Title")) {
                    BooksRow(book: book)
                }
            }
        }
    }
}

struct BookModel: Hashable {
    var title: String?
    var author: String?
}

struct BooksRow {

    let book : BookModel
    var body: some View {

        VStack {
            Text(book.title ?? "Unknown Title")
                .font(.headline)
            Text(book.author ?? "Unknown Author")
                .foregroundColor(.secondary)
        }
    }
}

错误:在“NavigationLink”上引用初始化程序“init(destination:label:)”要求“BooksRow”符合“View”

【问题讨论】:

    标签: ios list swiftui


    【解决方案1】:

    这里是修复

    struct BooksRow: View { // << here !!
    
        let book : BookModel
        var body: some View {
    
        // ... your other code
    

    【讨论】:

      【解决方案2】:
      struct BooksRow: View { // Now it conforms to View-Protocol
      
              let book : BookModel
              var body: some View {
      
                  VStack {
                      Text(book.title ?? "Unknown Title")
                          .font(.headline)
                      Text(book.author ?? "Unknown Author")
                          .foregroundColor(.secondary)
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        相关资源
        最近更新 更多