Swift – How to set initial value for NSUserDefaults

Swift 3 syntax example

Register a boolean default value:

UserDefaults.standard.register(defaults: ["SoundActive" : true])

And to get the value:

UserDefaults.standard.bool(forKey: "SoundActive")

Sidenote: Although the above code will return true, note that the value isn’t actually written to disk until you set it:

UserDefaults.standard.set(true, forKey: "SoundActive")

Leave a Comment