How to add value of variables to the value of a saved variable in userdefaults

Asked

Viewed 59 times

0

I will 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 recharge variable, let’s say that the value of the recharge was 70.0, so I need to be calculated to the value already existing in the variable balance and update the label, which in case would be 120.0.

I don’t have much experience with OO and I’m having a lot of difficulties, I believe I need to read first the value saved in the userdefaults of the balance variable and then increase it with the value of the recharge variable, sorry if I’m being too lay, could someone give me a direction? Because the user can perform several refills and in this the amount of the balance is changed

1 answer

1

It would look something 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.