3

I am trying to save an integer so it shows up after I switch the page or close the game. I made this to change the number but how do I save that number when I switch pages and load it when I go back to that page.

Change Code:

@IBAction func MoneyPress(sender: AnyObject) {

Money += 1

var MoneyNumberString:String = String(format: "Dollars:%i", Money)
self.DollarsLabel.text = (string: MoneyNumberString)

}

2 Answers 2

4

If it isn't a lot of data, the strategy I use to save data, pass it between pages, and persist it between app runs is to store the value in NSUserDefaults.

Setting A Value: When you first get or when you change the data, store it in NSUserDefaults.

@IBAction func MoneyPress(sender: AnyObject) {
    Money += 1
    var MoneyNumberString:String = String(format: "Dollars:%i", Money)
    self.DollarsLabel.text = (string: MoneyNumberString)
    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //This class     variable needs to be defined every class where you set or fetch values from NSUserDefaults
    defaults.setObject(MoneyNumberString, forKey: "money")
    defaults.synchronize() //Call when you're done editing all defaults for the method.
}

Loading A Value: When you need to get the values, just grab it from NSUserDefaults.

@IBAction func loadButton(sender: UIButton) {
    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var money = defaults.valueForKey("money") as? String
    dollarLabel.text! = money
}

To remove the stored data, all you need to do is call the removeObjectForKey function for each key previously set.

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("money")
defaults.synchronize()

Helpful Source on NSUserDefaults:

NSUserDefulats Class Reference: Link here.

Sign up to request clarification or add additional context in comments.

8 Comments

I am a little confused on the fetching a value part. Is there a way to have a save button which would save the data of the Dollar Label and then have a load button to bring it back to what was saved?
The answer depends on whether or not the label is in the same view as the button or not. See if my edited code in the "Loading A Value" section helps.
It works now but there is one more problem. When I load it up and then I press the button to add the money, it starts from 0 again instead of the number that it showed. Im not sure but that might have had to do with how it made me put a ! at the end of money on dollarLabel.text! = money!
Set the Dollar Label to equal the saved value in viewWillAppear. It will check the defaults every time the user goes to that view. You can add viewDidApper with this code: override func viewWillAppear(animated: Bool) { let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults(); var money = defaults.valueForKey("money") as? String; dollarLabel.text! = money; super.viewWillAppear(animated)}. Also, if you liked this answer and found it useful, please utopia it and select is as the right answer.
First, it made me add and ! after money. Then, I tried it out and it crashed when I tried to switch views. And yes I will click that it is the right answer when it works for me.
|
4

You can use NSUserDefaults for this.

Save Value

NSUserDefaults.standardUserDefaults().setInteger(money, forKey: "MoneyKey");

Retrieve Value

NSUserDefaults.standardUserDefaults().integerForKey("MoneyKey");

So can retrieve the value in viewDidLoad and load the data:

override func viewDidLoad()
{
    super.viewDidLoad()
    loadWebView()
    var money = NSUserDefaults.standardUserDefaults().integerForKey("MoneyKey");
}

When you come to the view for the first time the value of money will be 0.


Remove Value

If you need to remove a value from NSUserdefaults, you can use:

NSUserDefaults.standardUserDefaults().removeObjectForKey("MoneyKey")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.