I'm writing a bit of code to save user preferences.
enum CustomSettingsEnum: NSNumber {
case Off
case On
}
let someKey = "SOME_KEY"
func getSettings() -> CustomSettingsEnum {
var cseBool = Bool?
var cse: CustomSettingsEnum!
let settingsExists = NSUbiquitousKeyValueStore.defaultStore().boolForKey(self.someKey)
if settingsExist {
// Get the NSNumber and convert that to Bool
} else {
cse = CustomSettingsEnum.On
}
return cse
}
func setSettings(cse: CustomSettingsEnum) {
NSUbiquitousKeyValueStore.defaultStore().setObject(cse, forKey: self.someKey)
}
I'm calling setSettings() before ever calling getSettings(),
but when calling getSettings(), it seems settingsExist in getSettings() is always false. Why would this be? And is this the best way of saving such info?
Sorry, I misunderstood the function
boolForKeyforNSUbiquitousKeyValueStore.boolForKeyisn't checking whether or not that key exists, but is checking for the value of that key, which is supposed to be a Bool.So I rewrote it like this: enum CustomSettingsEnum: NSNumber { case Off case On }