我只是不知道如何将项目的核心数据值从 NavigationLink 中的 ForEach 列表传递到详细信息视图。这是出现错误的代码:“无法将'FetchedResults.Element'(又名'FileEnt')类型的值转换为预期的参数类型'FileViewModel'”
struct FileList: View {
@Environment(\.managedObjectContext) var context
@FetchRequest(entity: FileEnt.entity(), sortDescriptors: [NSSortDescriptor(key: "fileName", ascending: false)]) var results: FetchedResults<FileEnt>
@ObservedObject var fileData : FileViewModel
var body: some View {
NavigationView {
List {
ForEach(results) { aFile in
NavigationLink(destination: FileDetails(fileData: aFile), label: {
// ** error on red-underscored aFile above.
Text(aFile.fileName ?? "")
})
}
}.navigationBarTitle("Files")
}
}
}
FileViewModel 用于添加、编辑和详细信息视图。这是该问题的简化版本:
class FileViewModel: ObservableObject {
@Published var fileName = ""
init(){
}
func DetailItem(fileItem: FileEnt){
fileName = fileItem.fileName ?? ""
}
}
文件详情:
struct FileDetails: View {
@ObservedObject var fileData : FileViewModel
@Environment(\.managedObjectContext) var context
@State var isEdit = false
var body: some View {
VStack {
Form {
HStack {
Text("File Name:")
Spacer()
Text(fileData.fileName)
....
修改帖子以便于理解。感谢您的任何建议。