How to sum variable value with the value of a variable saved in userdefaults

0

I try to be objective, I need to save a variable that the value that is stored inside will be the text of a label and this value is changed when an input of another variable is performed. For example:

Variable balance has the value of 50.0, the user can perform an input that is saved in the reload variable, let's say the reload value was 70.0, so I need to be calculated to the existing value in the balance variable and update the label, which in this case would be 120.0.

I do not have much experience with OO and I'm having a lot of difficulties, I believe I need to first read the value saved in the userdefaults of the balance variable and then increment with the value of the reload variable, sorry if you're being too lazy, could someone give me one direction? Because the user can perform several recharges and in that the value of the balance is changed

    
asked by anonymous 11.09.2017 / 22:38

1 answer

1

It would look more like this ...

var saldo = NSUserDefaults.standardUserDefaults() 

let valorInicial = "50" 
saldo.setObject(valorInicial, forKey: "Valor")

//Salve a informação 
saldo.synchronize()


//Carregar Informação
var VL = NSUserDefaults.standardUserDefaults()
let Dados = VL.objectForKey("Valor")
//Some os valores e converta
var inteiros = Int(Dados) + Int(input.text) //input é o nome do campo que o 
//usuário irá digitar o segundo valor.

print(Inteiros)



//Make some changes
let novoValor = Inteiros
NV.setObject(novoValor, forKey: "Update")
//Lembre de salvar
NV.synchronize()
    
12.09.2017 / 13:40