0

I am trying to save an integer variable in swift/xcode so that when the user opens the application, the number they are incrementing stays the sames (is saved). So far, no luck with this code. The number always goes back to zero when I relaunch the app. The button below simply increments the smoke variable by 1, I want to save that data.

Thank you for your time.

let defaults = UserDefaults.standard
var smoke = 0

@IBAction func incrementSmoke(_ sender: UIButton) {
    smoke+=1

    defaults.set(smoke, forKey: "smoke")

    numDaysLabel.text = String(defaults.integer(forKey: "smoke"))        
}
7
  • 1
    You aren't retrieving the previous session's value of "smoke" from UserDefaults at any point in the code you've shown. Commented Dec 19, 2019 at 0:12
  • What do you mean by the previous "session"? Commented Dec 19, 2019 at 0:23
  • I think I know what you mean, basically I want the label to stay the same after each session, how do I do this? Commented Dec 19, 2019 at 0:32
  • 4
    From the previous time you ran the app. You save the value to UserDefaults, but then you have to assign the saved value to the variable when you restart the app if you want to use it. Probably just need to put smoke = defaults.integer(forKey: "smoke") in the init method. Commented Dec 19, 2019 at 0:32
  • So do I have to save something to the "smoke" variable for next time? Ok thanks Commented Dec 19, 2019 at 0:35

3 Answers 3

2

In an appropriate spot you should be initializing your smoke property with the value stored in UserDefaults. For example, you might want to do this in viewDidLoad, or you could just do it directly in the initializer where you declare the property.

var smoke = UserDefaults.standard.integer(forKey: "smoke")

@IBAction func incrementSmoke(_ sender: UIButton) {
    smoke += 1    
    UserDefaults.standard.set(smoke, forKey: "smoke")    
    numDaysLabel.text = String(smoke)
}

Note integer(forKey:) will return 0 by default if there is no value stored in your UserDefaults already for that key. If you want to set an initial value for the key, register(defaults:).

Note, since your value is supposed to be an integer, you might as well use integer(forKey:) instead of object(forKey:) so you don't have to deal with an optional / Any.

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

Comments

1

Swift 4

Set Value

UserDefaults.standard.set("Value", forKey: "Key") //setString

Get

UserDefaults.standard.string(forKey: "Key") //getString

Remove

UserDefaults.standard.removeObject(forKey: "Key")

In your viewDidLoad

smoke = UserDefaults.standard.string(forKey: "smoke") ?? 0

Then

numDaysLabel.text = "\(smoke)" 

Comments

0

You can use this one line of code to get data from UserDefaults. If there is data then it is returned, or it returns 0.

let smoke = UserDefaults.standard.object(forKey: "smoke") ?? 0

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.