简体   繁体   English

结构的计算属性中的访问环境 object 属性

[英]Access environment object property within a struct's computed property

I currently have a struct, which has a computed property called "answerArray".我目前有一个结构,它有一个名为“answerArray”的计算属性。 I want to compare this array of values with another array of values that is stored in an Environment object.我想将此值数组与存储在环境 object 中的另一个值数组进行比较。

struct CandidateModel {
@EnvironmentObject var model: CardViewModel

let index: Int
var name: String
var answers: [AnswerModel]
var answerArray: [Double] {
    get {
        var arr: [Double] = []
        for i in answers {
            if i.isKeyIssue {
            // If answer is a key issue
                arr.append(i.score * 1.5)
                // Apply multiplier and append to answer array
            }
            else {arr.append(i.score)}
            // otherwise just append to list
        }
        return arr
    }
}
var scoreArray: [Double] {
    get {

        var compScoreArray: [Double] = []

        for i in 0...self.model.getAnswerList().count - 1 {
            let questionDifference = abs(self.answerArray[i] - self.model.answerList[i])
            compScoreArray.append(questionDifference)
        }

        return compScoreArray
    }
}

I when attempt to access this property I get the following error:我在尝试访问此属性时收到以下错误:

Thread 1: Fatal error: No ObservableObject of type CardViewModel found. A View.environmentObject(_:) for CardViewModel may be missing as an ancestor of this view.

I am simply trying to update the scoreArray property in the CandidateModel as the answerList property in my environment object changes.我只是想更新 CandidateModel 中的 scoreArray 属性,因为我的环境 object 中的 answerList 属性发生了变化。 How could I better implement this?我怎样才能更好地实现这一点?

Here is my View Code:这是我的查看代码:

struct SentimentComparisonBarView: View {
@EnvironmentObject var model: CardViewModel

var body: some View {
    HStack {
      // 2
        Image("Jobs_Economy")
            .renderingMode(.template)
            .resizable()
            .scaledToFit()
            .frame(width: 50)
            .foregroundColor(Color.init(#colorLiteral(red: 0.3942148186, green: 0.3146163453, blue: 0.4587302703, alpha: 1)))

        Text("Jobs/Economy")
            .frame(width: 120, alignment: .leading)
          // 3
          Rectangle()
            .fill(Color.init(#colorLiteral(red: 0.5201314092, green: 0.3027745485, blue: 0.49252671, alpha: 1)))
            .frame(width: CGFloat(2 * 10.0), height: 5.0)
          // 4
          Spacer()
            Text("Low")
                .padding(.horizontal, 10)

        Button("Test") {
            print(self.model.presidentialCandidates[0].scoreArray)
        }

    }
}

You should use @EnvironmentObject in your view and pass it down to your model.您应该在视图中使用@EnvironmentObject并将其传递给您的 model。 And don't forget to call .environmentObject to inject your CardViewModel in SceneDelegate .并且不要忘记调用.environmentObject将您的CardViewModel注入SceneDelegate For exemple:举个例子:

let myView = MyView().environmentObject(CardViewModel())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM