UserDefaults() is a great way to store pieces of data. However, they use string identifiers to access/save that data. String ID’s are a pain to remember and a pain to input as a typo means they won’t work properly. Here is a simple solution:

let defaults = UserDefaults()

var resetHour: Int { //1 get { // 2 return defaults.integer(forKey: "ResetHourInt") } set { // 3 defaults.set(newValue, forKey: "ResetHourInt") } }

What’s going on here?

You create a variable. This will be used for both accessing and storing the data in UserDefault. Anytime you use ‘resetHour’ it will get the value from UserDefaults. Any time you change the value of ‘resetHour’ it will update the UserDefaults value. (Note: the ‘newValue’ property is value you set ‘resetHour’ to. )

So anytime you need to do anything with UserDefaults you are just using your variable. No muss, no fuss.