如何一起使用 NSUbiquitousKeyValueStore 和 NSUserDefaults
文档不清楚如何在边缘情况下使用 NSUbiquitousKeyValueStore
。
如果我想设置一个值,我知道我应该为 NSUserDefaults
和 NSUbiquitousKeyValueStore
设置一个值,因为 iCloud 可能被禁用。然而,在我的测试中,即使 iCloud 被禁用,[NSUbiquitousKeyValueStore defaultStore]
也会返回一个有效的对象(在 Mac OS 上测试)。
另外,据我了解,如果启用了 iCloud,NSUbiquitousKeyValueStore
的值将存储到磁盘(并且可以离线使用)。如果您确定数据少于 64KB,那么使用 NSUserDefaults 的原因是什么?
Documentation is not clear on how to use NSUbiquitousKeyValueStore
with edge cases.
If I want to set a value, I understand that I should set a value to both NSUserDefaults
and NSUbiquitousKeyValueStore
since iCloud could be disabled. However in my tests [NSUbiquitousKeyValueStore defaultStore]
return a valid object even if iCloud is disabled (tested on Mac OS).
Also, to my understanding is that if iCloud is enabled, NSUbiquitousKeyValueStore
's values are stored to disk (and available offline). What are the reason to use NSUserDefaults if you are sure that you have less than 64KB of data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在使用
http://blog.mugunthkumar.com/coding/ios-code-mkicloudsync-sync-your-nsuserdefaults-to-icloud-with-a-single-line-of-code/
这是一个由 Mugunth Kumar 编写的简单类(感谢!),可以为您完成工作...除非您有特殊需求,添加一行代码,它将完成对 iCloud 的所有读取和写入操作...您需要的一切要做的就是阅读并像往常一样写入 NSUserDefaults...
编辑:
Carful,如果您从 NSUserDefaults 中删除一个项目,我上面链接到的代码将不会从云中删除该项目。每当您从 NSUSerDefaults 中删除项目时,请对 NSUbiquitousKeyValueStore 执行相同的操作,如下所示:
I am using
http://blog.mugunthkumar.com/coding/ios-code-mkicloudsync-sync-your-nsuserdefaults-to-icloud-with-a-single-line-of-code/
It is a simple class written by Mugunth Kumar (thanx !) that does the work for you... Unless you have special needs, add one line of code and it will do all the reading and writing to iCloud... all you need to do is read and write to NSUserDefaults as usual...
Edit:
Carful, if you remove an item from NSUserDefaults the code I linked to above will not remove the item from the cloud. Whenever you remove an item from NSUSerDefaults please do the same to NSUbiquitousKeyValueStore as So:
如果您想同步所有 NSUserDefaults,上面提供的 Mugunth Kumar 答案效果非常好!
然而,这是一种全有或全无的方法。您无法挑选或选择您想要同步的默认值。
我发现这个教程如果您希望更加挑剔,它可能会有所帮助。
The Mugunth Kumar answer provided above works beautifully if you want to sync all of your NSUserDefaults!
However, it is an ALL or NOTHING approach. You can not pick or choose the defaults you wish to sync.
I found this Tutorial that may be of assistance if you are looking to be more picky.
我编写了一个简单的 iOS 类别,可用于将值保存到 NSUbiquitousKeyValueStore 中,同时存储到 NSUserDefalt 中。
希望这有帮助:
https://github.com/RiccardoPaolillo/NSUserDefault-iCloud
I wrote a simple iOS category that can be used to save a value also into NSUbiquitousKeyValueStore while storing into NSUserDefalt.
hope this help:
https://github.com/RiccardoPaolillo/NSUserDefault-iCloud