Xcode Swift 5.0为存储在EnvironmentObject中的数组对象赋值的最佳方法

Xcode Swift 5.0为存储在EnvironmentObject中的数组对象赋值的最佳方法,xcode,xcode11,swift5,Xcode,Xcode11,Swift5,我有一个名为Questions的结构模型,它存储在一个带有@Published方法的observeObject类中 struct Question: Hashable, Codable, Identifiable { var id: Int var questionText: String var questionComment: String } class DataRep: ObservableObject { @Published var showFa

我有一个名为Questions的结构模型,它存储在一个带有@Published方法的observeObject类中

struct Question: Hashable, Codable, Identifiable {
    var id: Int
    var questionText: String
    var questionComment: String

}

class DataRep: ObservableObject  {
    @Published var showFavoritesOnly = false
    @Published var QuestionList : [Question] = QuestionListData
    @Published var selectedCategory = "all"
}
稍后在列表视图中,我阅读问题并添加到列表中,在导航中,我将问题传递到存储在@State中的新视图

struct QuestionCommentView: View {
    @State var question : Question
    var body: some View {
        NavigationLink(destination:QuestionCommentEntryView(question: question))
        {
            Text(question.questionText)
            Text(question.questionComment)
       }
    }
}
在QuestionCommentryView中,我有一个文本字段,在这里我通过单击按钮读取“注释数据”,并希望将其与数组一起存储在EnvironmentObject中,以便其他视图可以访问更新的注释

import SwiftUI

struct QuestionCommentEntryViewTest: View {
    @EnvironmentObject var datarep: DataRep  //forced to declare
    @State var question : Question
    @State private var enteredComment = ""

    //Binding
    var body: some View {
        VStack{
            TextField("Comment", text: $enteredQuestionComment)
            Button (action: {
                self.AddScaleComment ()
            })
            {Text("Add Comment")}

        }
    }

    func AddScaleComment ()
    {
        let questions : [Question] = datarep.QuestionList
        ForEach (questions) {q in
            if q.id = question.id
            {
                let quest.questionComment = self.enteredComment
            }
        }
    }
}
问题1:有没有更简单的方法来实现这种逻辑

问题2:我如何确保QuestionCommentView上的更新刷新,因为我想显示更新的注释

import SwiftUI

struct QuestionCommentEntryViewTest: View {
    @EnvironmentObject var datarep: DataRep  //forced to declare
    @State var question : Question
    @State private var enteredComment = ""

    //Binding
    var body: some View {
        VStack{
            TextField("Comment", text: $enteredQuestionComment)
            Button (action: {
                self.AddScaleComment ()
            })
            {Text("Add Comment")}

        }
    }

    func AddScaleComment ()
    {
        let questions : [Question] = datarep.QuestionList
        ForEach (questions) {q in
            if q.id = question.id
            {
                let quest.questionComment = self.enteredComment
            }
        }
    }
}
问题3:为什么ForEach会给出以下错误。我使用了我在我的应用程序的其他页面中使用的相同语法。错误包括: (a) 使用未声明的类型“问题” (b) 一行中的连续语句必须用“;”分隔

问候,


M

而不是
@State
(只能用作私有内部视图状态),将
问题作为
@Binding
传递(调用方,如快照中未提供,应需要相应修改)

示意图上应该是

struct QuestionCommentView: View {
    @Binding var question : Question
    ...

    NavigationLink(destination:QuestionCommentEntryView(question: $question))

因此,您可以在中准确地修改原始的
问题

func AddScaleComment ()
{
    self.question.questionComment = self.enteredComment
}
所以,当它绑定到原始数组中的值时,它也将被更新