How to sum observed int data?

Hi! I'm working on an app where the user chooses 11 times an menu option, and after that the user chooses, the intention is that depending on what they chose, that there is a calculation of multiple int data and the result will be shown as text on the screen.

Currently every menu option is done. I've added an observable int file and when the preferred option is selected the int data that I've chosen will be changed.

So now I've got a lot of observable/observed int data but I don't know how to sum the int data.

Can you help me?


This is how I set-up the @observable data files:

import Foundation
import SwiftUI

class Select1Variable: ObservableObject {

   
    @Published var Select1Variable = "Selecteer..."

}
Answered by Claude31 in 745687022

'm not sure to have a full understanding of your code, but I would suggest:

  • create a single class for ObservableObject, with all the observed value:
class ValueVariableVUUR: ObservableObject {

    @Published var vuurZonValue = 0     // var name should start with lowercase
    @Published var vuurMaanValue = 0

    func sumValues() -> Int {
        return vuurZonValue + vuurMaanValue
    }
}

Pass the ValueVariableVUUR instance to the environment and use it in any view you need (see here is you need some info on using environment https://developer.apple.com/forums/thread/725355)

how to sum the int data

Where are the data stored (I do not see in code)

.

I've got a lot of observable/observed int data

Please show which are all those var

If they are stored in an array, reduce() is a nice way to compute the sum. But we need to see more to really understand the question.

Hi! Currently I have two observable objects made in two different Swift Files.

File 1:

import Foundation
import SwiftUI

class ZonValueVariableVUUR: ObservableObject {


    @Published var VUURZonValue = 0

}

File 2:

import Foundation
import SwiftUI

class MaanValueVariableVUUR: ObservableObject {

    @Published var VUURMaanValue = 0

}

.

When I choose the option "Ram" in menu 1, the VUURZonValue will turn 18. I've done this with the code below that I added in the action-section of a button that's part of a menu.

ZonValueVUURFunc.VUURZonValue = 18

When I choose option "Leeuw" in menu 1, the VUURMaanValue will turn 15. I've done this the same way as above with the code below that I added in the action-section of a button that's part of a menu.

MaanValueVUURFunc.VUURMaanValue = 15

.

So to summarize, the VUURZonValue & VUURMaanValue are normally 0, but if you choose as user the option "Ram", VUURZonValue will change to 18, and if you choose "Leeuw" at VUURMaanValue the 0 will change in to 15.

So what do I do now to count the VUURZonValue and VUURMaanValue together and display this as text that get's updated if there is a change?

Accepted Answer

'm not sure to have a full understanding of your code, but I would suggest:

  • create a single class for ObservableObject, with all the observed value:
class ValueVariableVUUR: ObservableObject {

    @Published var vuurZonValue = 0     // var name should start with lowercase
    @Published var vuurMaanValue = 0

    func sumValues() -> Int {
        return vuurZonValue + vuurMaanValue
    }
}

Pass the ValueVariableVUUR instance to the environment and use it in any view you need (see here is you need some info on using environment https://developer.apple.com/forums/thread/725355)

How to sum observed int data?
 
 
Q