NSUserDefaults和iOS框架/库 [英] NSUserDefaults and iOS framework / library

查看:84
本文介绍了NSUserDefaults和iOS框架/库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用[NSUserDefaults standartdUserDefaults]构建一个应用程序,以在本地存储各种信息(有关会话,用户首选项等).

I have recently been building an app using [NSUserDefaults standartdUserDefaults] to store locally various information (regarding the session, user preferences, etc).

现在,我将项目的一个子部分导出为iOS框架(目标是制作一个SDK).

Now I am exporting a subpart of my project as an iOS framework (the goal is to make a SDK).

我正在生成要导入到第三方应用程序中的MyKit.framework和MyKit.bundle(该捆绑包包含Storyboard,Localizable.strings和.xcassets).

I am generating a MyKit.framework and MyKit.bundle to be imported in third party apps (the bundle contains Storyboards, Localizable.strings and .xcassets).

目前,我确保MyKit.framework使用的资源是从MyKit.bundle范围而不是[NSBundle mainBundle]加载的,以避免与第三方应用程序的资源发生冲突.

For the moment, I made sure that resources used by MyKit.framework are loaded from the MyKit.bundle scope and not the [NSBundle mainBundle] to avoid collisions with resources from the third party app.

现在,如果在框架和目标应用之间共享某些键,NSUserDefault也可能导致冲突.

Now, NSUserDefault can also leads to collisions if some keys are shared between the framework and the target apps.

- initWithSuiteName:是否可以解决? 我可以给NSUserDefaults一个作用域吗? 我应该创建一个包装器来访问自定义NSUserDefaults域吗?

Is - initWithSuiteName: the appropriate work around? Can I give a scope to NSUserDefaults ? Should I create a wrapper to access a custom NSUserDefaults domain ?

推荐答案

我自己正在研究相同的问题.

I'm working on the same problem myself.

我目前的观察是,由于NSUserDefaults standardDefault包含对象的层次结构(如.plist),因此您可以将所有SDK的默认键/值放在字典"条目中(例如,以您的SDK的域-"com.mydomain.frameworkname").这样会将您的物品装箱,并将其与应用程序自身的值区分开.

My current observation is, since NSUserDefaults standardDefault contain a hierarchy of objects (like a .plist) you can put all your SDK's default key/values inside some "Dictionary" entry, (e.g. named after your SDK's domain - "com.mydomain.frameworkname"). That will box your items and set them apart from the app's own values.

这里有一个缺点-KVO仅适用于顶级键/值更改,因此您将无法在NSUserDefaults中直接注册默认值更改.

There is a downside here - that KVO will only work for top-level key/value changes, so you won't be able to "register" for default-value-changes done directly in the NSUserDefaults.

然后,将SDK的出厂默认值"首次"加载到NSUserDefaults standardDefaults中.为此,有一个标准的行为和API,称为"registerDefaults".

Then, there's the part of "first time" loading of "factory default values" for the SDK, into the NSUserDefaults standardDefaults. For that, there is a standard behaviour and API, called "registerDefaults".

您可以在SDK的捆绑包中拥有一些.plist资源,称为"factoryDe​​faults.plist",然后在初始化SDK时,将执行以下操作:

You can have some .plist resource in your SDK's bundle, called "factoryDefaults.plist" and then, in the initialisation of your SDK, you'll do something like this:

NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"factoryDefaults" ofType:@"plist"];
NSDictionary* factorySettings = [NSDictionary dictionaryWithContentsOfFile:path];
[[NSUserDefaults standardDefaults] registerDefaults:factorySettings];
[[NSUserDefaults standardDefaults] synchronize];

然后,您可以使用标准的用户默认值API继续使用默认值,可以根据需要更新它们,并且仍保持托管应用程序的默认值干净.

Then you can continue working with your default values, using standard user-defaults APIs, you can update them as needed, and still keep your hosting app's defaults kind-of-clean.

在可能会覆盖NSUserDefaults函数的类中,可以在NSUserDefaults standardDefaults中隐藏挖掘SDK的单个条目的麻烦,并且可以访问SDK的条目而不是顶级standardDefaults.

It is possible to hide the nuisance of digging into your SDK's single entry in NSUserDefaults standardDefaults, in a class that will kind-of override NSUserDefaults functions, and will access the SDK's entry instead of the top-level standardDefaults.

警告:我现在才这样做-尚不能报告成功.因此,对于任何批评者和建议,我都会感激不尽,欢迎您尝试这种方法.

Word of caution: I'm only doing this now - can't yet report of success. So I will be thankful for any critic and recommendations, and you're welcome to try this method.

这篇关于NSUserDefaults和iOS框架/库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆