Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Declared string inside class keeps reverting to original value

Brand new to swift.

Im 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)
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading