无法将类型“[Document]”的值转换为预期参数类型“Binding<C>”

问题描述 投票:0回答:2

此错误 无法将类型“[Document]”的值转换为预期参数类型“Binding” 发生在嵌套

ForEach
循环中。我的想法是深入研究数组,直到我可以访问属性。

这是代码:

型号

import Foundation

struct Publisher: Identifiable {
    var id = UUID()
    var username: String
    var documents: [Document]
}

struct Document: Identifiable {
    var id = UUID()
    var title: String
    var description: String
    var username: String
    var userImage: String
    var text: String
    var date: Date
}

视图模型


import Foundation

final class PublisherViewModel: ObservableObject {
    @Published var publishers = [
        Publisher(
            username: "blah",
            documents: [Document(
                title: "Blah Strikes Again",
                description: "How Blahs Work",
                username: "blah",
                userImage: "blah",
                text: "Enim porro et eius et. Recusandae nisi aperiam tenetur dolore ut ratione. Repudiandae quas sit est molestiae exercitationem non sequi et.",
                image: "",
                audio: "",
                video: "",
                date: Date())
    ]
}

import SwiftUI

struct PublisherDetailView: View {
   
    @ObservedObject var viewModel = PublisherViewModel()
    
    var body: some View {
       HStack {
          ForEach(viewModel.publishers) { publisher in
              ForEach(publisher.documents) { document in // <-- **error here**
                  Text(document.content)
                      .padding()
                  Spacer()
                }
            }
        }
    }
}

我读过一些关于嵌套

ForEach
循环的文章,其中大多数都是尝试循环遍历单个属性与数组(或在某些情况下是字典)时的错误。

我猜这是 Swift 允许的。我还假设编译器认为我想使用

<Binding>
因为它无法识别我的数组/实现?

也不知道无法推断通用参数“C”`意味着什么

generics swiftui foreach
2个回答
0
投票

我也遇到了同样的错误。 就我而言,我重命名了模型中的一个属性,但忘记在另一个文件中的一个位置更新它。这是给我错误的文件。

@Model
final class Person {
    @Attribute(.unique) let name: String

    //this property was before `birthdate`
    let birthDate: Date 

    let creationDate: Date
    var updateDate: Date

    init(
        name: String,
        birthDate: Date,
        updateDate: Date = Date()
    ) {
        self.name = name
        self.birthDate = birthDate
        self.updateDate = updateDate
        self.creationDate = Date()
    }
}

另一个文件看起来像这样

struct PersonListView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var persons: [Person]

    var body: some View {
        List {
            // at the below line is where Xcode was throwing the error
            ForEach(persons) { person in
               VStack(alignment: .leading) {
                  Text("\(person.name)")

                  // this is where I had `person.birthdate` 
                  Text("\(person.birthDate, format: Date.FormatStyle(date: .abbreviated, time: .none))")
               }
            }
            .onDelete(perform: deleteItems)
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            for index in offsets {
                modelContext.delete(persons[index])
            }
        }
    }
}

-1
投票

想通了

我使用的是

id
参数。但现在我的观点都很时髦,但那是另一个问题了。

© www.soinside.com 2019 - 2024. All rights reserved.