Home > Blockchain >  Declared string inside class keeps reverting to original value
Declared string inside class keeps reverting to original value

Time:11-27

Brand new to swift.

I'm running into a problem where a declared string inside a class keeps reverting back to its original value after I modify it.

class Calculation: ObservableObject {
    var CalcString: String = "test"
    
    func modifyCalcString(newData: String) {
        CalcString = CalcString   newData
        print(CalcString)
    }
}

modifyCalcString is called using a button in an app view

Calculation().modifyCalcString(newData: digit)

(digit is a string)

my problem is every time I call modifyCalcString it is modified and then reverts back to its original value.

for example when calling modifyCalString with newData as "5" the print statement inside the function will output "test5" but after printing it again it reverts back to "test"

im pretty lost here and would appreciate the help.

CodePudding user response:

The only reason for this is that you are not working with the same class instance every time.

  • Related