Search code examples
iosswifticloud

App crash using iCloud Key-Value storage writing


My app crashes every time I try to set a value for a key using iCloud key-value storage...

I get this error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason:     '[<NSUbiquitousKeyValueStore 0x13fd5b8a0> setValue:forUndefinedKey:]: this class     is not key value coding-compliant for the key ladies.'
*** First throw call stack:
(0x1842f0f48 0x19979bf80 0x1842f0c08 0x18516c014 0x1000e5380 0x1000e9914     0x1000f0430 0x1000f046c 0x1000f04a0 0x1000f04e4 0x1898bf240 0x1898bed3c     0x1898bebc4 0x189085c2c 0x101429c68 0x10142f710 0x1842a81f8 0x1842a6060 0x1841d4ca0 0x18f758088 0x1898ecffc 0x1000f4564 0x199fde8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I define my keystore:

var iCloudStore = NSUbiquitousKeyValueStore.defaultStore()

Set my value like this:

iCloudStore.setValue(experience, forKey: "experience")

Why is this not working? Do I have to define that key for it to work? I really don't understand the error i get...


Solution

  • setValue:forKey: is not a method on NSUbiquitousKeyValueStore.defaultStore() used to save data to an iCloud key-value store. Instead, it is the method from the NSKeyValueCoding protocol that is used for more general key-value coding with the types NSValue and NSNumber. The error you received refers to you attempting to access the “experience” key on your iCloudStore object itself rather than in the iCloud key-value store.

    The methods available on NSUbiquitousKeyValueStore.defaultStore() to save key-value pairs are:

    • setArray:forKey:
    • setBool:forKey:
    • setData:forKey:
    • setDictionary:forKey:
    • setDouble:forKey:
    • setLongLong:forKey:
    • setObject:forKey:
    • setString:forKey:

    Choose the method that best fits your type.