How to implement EnvironmentObject in my project?

Hi! I'm working on an app where there are multiple menu's and if you chose an option, an observable object changes it value. This happens inside the menu via the action section. For example: vuur.vuurZonValue = 18.

Now I have placed these multiple menu's in different files because otherwise Xcode would give me an error that it takes too long to calculate.

My observable int data looks like this:

import SwiftUI

class VuurValues: ObservableObject {

    @Published var vuurZonValue = 0    
    @Published var vuurMaanValue = 0
    @Published var vuurAscedantValue = 0
    @Published var vuurVenusValue = 0
    @Published var vuurMarsValue = 0
    @Published var vuurJupiterValue = 0
    @Published var vuurSaturnusValue = 0
    @Published var vuurMCValue = 0
    @Published var vuurUranusValue = 0
    @Published var vuurNeptunesValue = 0
    @Published var vuurPlutoValue = 0
    @Published var vuurTotaal = 0


    func sumValues() -> Int {
        return vuurZonValue + vuurMaanValue + vuurAscedantValue + vuurVenusValue + vuurMarsValue + vuurJupiterValue + vuurSaturnusValue + vuurMCValue + vuurUranusValue + vuurNeptunesValue + vuurPlutoValue
    }


}

.

So when I call the sumValues() function I do it like this:

Text(String("\(vuur.sumValues())"))

FYI: 'vuur' is how i called the observed object in the file where I want to see the result. I've called the data via this text above below the struct line:

@ObservedObject var vuur = VuurValues()

.

But now the problem is that when I want to read the sum function of all the int data that changed based on the chosen options in the different menu's, it doesn't work.

When I insert the text (second box ↑) in the project where everything comes together I just keep getting 0. But when I insert the same text in the file of for example the first menu, it works but I can only change the value of that menu and not the others.

Can you help me?

Answered by meesakveld in 745981022

I've fixed it!

Accepted Answer

I've fixed it!

How to implement EnvironmentObject in my project?
 
 
Q