ios - 使用 iCloud Key-Value 存储写 App 崩溃

标签 ios swift icloud

每次我尝试使用 iCloud 键值存储为键设置值时,我的应用程序都会崩溃...

我收到这个错误:

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) 

我定义我的 keystore :

var iCloudStore = NSUbiquitousKeyValueStore.defaultStore()

像这样设置我的值:

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

为什么这不起作用?我是否必须定义该键才能工作?我真的不明白我得到的错误...

最佳答案

setValue:forKey: 不是 NSUbiquitousKeyValueStore.defaultStore() 上用于将数据保存到 iCloud 键值存储的方法。相反,它是 NSKeyValueCoding 协议(protocol)中的方法,用于使用 NSValueNSNumber 类型进行更通用的键值编码。您收到的错误是指您试图访问 iCloudStore 对象本身而不是 iCloud 键值存储中的“体验”键。

NSUbiquitousKeyValueStore.defaultStore() 上可用于保存键值对的方法有:

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

选择最适合您的类型的方法。

关于ios - 使用 iCloud Key-Value 存储写 App 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184486/

相关文章:

IOS View 仍然没有加载到正确的位置

swift - 如何以编程方式将 UILabel 和 UIImageView 添加到 CollectionView 中?

swift - 为什么在所有类型都已定义时需要泛型?

ios - swift : Parse PFQueryTableViewController - numberOfRowsInSection when PFObject is Array

ios - 我是否应该删除 NSUbiquitousKeyValueStoreDidChangeExternallyNotification 的通知

ios - swift ,Xcode 6 测试版 4 : How do I retrieve the file path of an image file that is currently being displayed using the Photos framework's assets?

objective-c - 如何在 iCloud 中保存 NSData

ios - UITextview 显示文本阿拉伯语但数字英语

ios - 无法快速从 Firebase 数据库检索数据。试图弄清楚它是我的 json 树还是我查询的不同方式

ios - 点击 tabBarItem 以显示 View Controller