0

I would like to perform some assignments to some global variables when a change is made to an @Published item in an ObservableObject.

I have multiple @Published variables in my ObservableObject. Is there a way to adjust all the @Published variables in an ObservableObject when only one @Published variable is modified by a view external to the ObservableObject?

This is basically the code I want to do:

class myObsObj: ObservableObject{
    @Published var variable1 = 2.1
    @Published var variable2 = 7.1
    
    When variable1 is changed, set variable2 = 5 + variable1
    
    
}
gary
  • 85
  • 7

1 Answers1

0

try this:

class MyObsObj: ObservableObject {
    // When variable1 is changed, set variable2 = 5 + variable1
    @Published var variable1 = 2.1  {
        didSet {
            variable2 = 5 + variable1
        }
    }
    @Published var variable2 = 7.1
}
  • Thanks much, workingdog. I knew it must be a simple thing:) – gary Jun 10 '21 at 07:00
  • if this answered your question, please click the "check mark" button under the vote buttons. – workingdog support Ukraine Jun 10 '21 at 07:57
  • LOL, the green checkmark. Got it now. One thing i don't understand is why I can't find a reference source for the swiftUI language. "didSet" would be easily identifiable as a method, or maybe a function, as part of the "@Published var" object. It should be easy to automatically list every property, modifier, and function for any object. Probably there is a way and I am just the last to know:) – gary Jun 10 '21 at 18:12