Cannot convert value of type '[Document]' to expected argument type 'Binding<C>'
This error Cannot convert value of type '[Document]' to expected argument type 'Binding' occurs in the nested ForEach
loop. My idea is to drill down into the arrays until I can access a property.
Here's the code:
Model
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
}
ViewModel
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()
}
}
}
}
}
I've read a few posts on nested ForEach
loops and most of them were errors in trying to loop over a single property versus an array (or in some cases a dictionary).
I'm guessing this is allowed by Swift. I also assume the compiler thinks I want to use a <Binding>
because it doesn't recognize my array/implementation?
Not sure what Generic parameter 'C' could not be inferred` means either.
Figured it Out
I was using the id
parameter. But now my views are all funky, but that's a different problem.
精彩评论