1

I currently have a struct, which has a computed property called "answerArray". I want to compare this array of values with another array of values that is stored in an Environment 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. 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)
        }

    }
}

1 Answer 1

1

You should use @EnvironmentObject in your view and pass it down to your model. And don't forget to call .environmentObject to inject your CardViewModel in SceneDelegate. For exemple :

let myView = MyView().environmentObject(CardViewModel())
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your input! I thought I already did this. I've added more snippets to my original question for you to reference.
@JoshuaMartinez I'm sorry but I don't understand what is your problem now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.