I have the code:
let userDefaults = NSUserDefaults.standardUserDefaults()
if let branchcheck = userDefaults.boolForKey("someBooleanValue") as Bool? {
println("found boolean as \(branchcheck)")
}
This is grand, only when nothing has been added to user defaults this automatically returns 'false'.
I want all settings to be 'true' by default.
How is this accomplished?
According NSUserDefaults/AccessingPreferenceValues Apple recommends to register all default values, for example
let userDefaults = NSUserDefaults.standardUserDefaults()
let appDefaults = ["someBooleanValue" : true]
userDefaults.registerDefaults(appDefaults)
this procedure does not write anything in the preference file, but those values are defined as the default values for the appropriate keys
Swift 3
let userDefaults = UserDefaults.standard
let appDefaults: [String:Any] = ["someBooleanValue" : true,
"someStringValue" : "foo"]
userDefaults.register(defaults: appDefaults)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With