如何一起使用 NSUbiquitousKeyValueStore 和 NSUserDefaults

发布于 2024-12-14 07:45:23 字数 414 浏览 9 评论 0原文

文档不清楚如何在边缘情况下使用 NSUbiquitousKeyValueStore

如果我想设置一个值,我知道我应该为 NSUserDefaultsNSUbiquitousKeyValueStore 设置一个值,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2024-12-21 07:45:23

我正在使用
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 执行相同的操作,如下所示:

NSUbiquitousKeyValueStore* keyValueStore=[NSUbiquitousKeyValueStore defaultStore];
[keyValueStore removeObjectForKey: yourKey];

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:

NSUbiquitousKeyValueStore* keyValueStore=[NSUbiquitousKeyValueStore defaultStore];
[keyValueStore removeObjectForKey: yourKey];
梦醒时光 2024-12-21 07:45:23

如果您想同步所有 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.

国际总奸 2024-12-21 07:45:23

我编写了一个简单的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文