Ios 应用程序崩溃使用iCloud键值存储写入
每次我尝试使用iCloud key-value storage为密钥设置值时,我的应用程序都会崩溃 我得到这个错误:Ios 应用程序崩溃使用iCloud键值存储写入,ios,swift,icloud,Ios,Swift,Icloud,每次我尝试使用iCloud key-value storage为密钥设置值时,我的应用程序都会崩溃 我得到这个错误: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSUbiquitousKeyValueStore 0x13fd5b8a0> setValue:forUndefinedKey:]: this class is not key value codi
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)
如下设置我的值:
iCloudStore.setValue(experience, forKey: "experience")
为什么这不起作用?我必须定义该键才能工作吗?我真的不明白我犯了什么错误
setValue:forKey:
不是NSUbiquitousKeyValueStore.defaultStore()上用于将数据保存到iCloud键值存储的方法。相反,它是来自NSKeyValueCoding
协议的方法,用于类型为NSValue
和NSNumber
的更通用的键值编码。您收到的错误是指您试图访问iCloudStore
对象本身而不是iCloud键值存储中的“体验”键
NSUbiquitousKeyValueStore.defaultStore()上可用的保存键值对的方法有:
setArray:forKey:
setBool:forKey:
setData:forKey:
setDictionary:forKey:
setDouble:forKey:
setLongLong:forKey:
setObject:forKey:
setString:forKey:
选择最适合您的类型的方法。因此,iCloud等效的NSUserdefauls.setValue是setObject?选择最适合您的类型的方法setObject:forKey:
可能是所有方法中最通用的方法,可能会起作用,但如果可能的话最好是具体的。在某些情况下,比如使用NSUserDefaults
,当您调用setValue:forKey:
时,您会得到与setObject:forKey:
相同的行为。要回答您最初的问题,是的,setObject:forKey:
,对于iCloud,代表了setValue:forKey:
在NSUserDefaults
上为您做的事情。
iCloudStore.setValue(experience, forKey: "experience")