如何从 Objective-c/Swift 中的 settings.bundle 中检索值?
我创建了一个项目,用于设置和检索settings.bundle 中的值。我还在 settings.bundle 文件中设置了一些默认值。现在的问题是,当我
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
loginName.text = [defaults objectForKey:@"login_name"];
第一次检索值时,它显示为 null,但这些值是在 iPhone 应用程序设置中设置的。 如果我更改值或手动设置它,则可以正确检索值。
帮帮我
I have created a project that set and retrieve values from settings.bundle. I have also set some defaults values in settings.bundle file. Now the problem is when I retrieve values as
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
loginName.text = [defaults objectForKey:@"login_name"];
for the first time it shows null, but the values get set in iPhone application settings.
If I change the values or set it manually, then values are retrieved properly.
Help me out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽管您定义了默认设置,但它们并未真正存储为值。它们被存储为默认值。如果您尝试读取它,则该值为空。默认设置是另一个属性,就像值一样。但这并不意味着将默认值写入为默认值。
我所做的是,首先,检查某些设置(我确信应该有一个值)是否存储了任何内容。如果它没有任何内容,那么我会编写所有默认值。
这是一个例子。
在 AppDelegate.m 上,我检查 email_notificaciones_preference 是否有值,如果没有,我将所有默认设置写入每个设置。
我用这个函数为每个元素写入默认值。
希望有帮助。
Although you define the defaults settings, they are not really stored as a value. They are stored as default. If you try to read it, the value is null. Default setting is another property as value is. But it doesnt mean that will write the default value as a default.
What I do is, first, check if some setting,(that I'm sure that should have a value) has anything stored on it. If it doesn't have anything then I write all the defaults.
Here is an example.
on AppDelegate.m I check if email_notificaciones_preference has a value, if not, I write ALL default settings to each setting.
This function is what I use to write defaults to each element.
Hope that helps.
如果有人需要它 - 我将答案从 MIQUEL 翻译为 Swift(尽我所能,因为我仍在学习):
以及 func registerDefaultsFromSettingsBundle:
If anyone needs it - I translated the answer from MIQUEL to Swift (as good as I could as I'm still learning) :
And the func registerDefaultsFromSettingsBundle:
更新为 Swift 3:
Updated for Swift 3:
swift 2.1 的更新版本:
Updated version for swift 2.1:
您可以使用像这样的简单属性包装器:
用法
注意,只需在变量前添加
@objc
即可 100% 兼容 Objective-C。其背后的代码实现:
设置捆绑包值位于
UserDefaults
中,因此您可以为其使用自定义PropertyWrapper
。以下包装器适用于任何UserDefault
值,包括SettingsBundle
的所有值。属性包装器
根和子级
您应该为根和子级
plist
传递以下枚举:如果需要,查找并设置默认值。
此包装器使用此函数查找捆绑包键的默认值:
此包装器可以将任何类型的可编码存储到用户默认值中或从用户默认值恢复,包括已经符合可编码的所有 Swift 标准数据类型。
此外,您还可以找到一个类似但代码更少的版本,用于从任何用户默认值访问任何键值,您可以查看这个答案在这里
You can use a simple property wrapper like this:
Usage
Note that this is 100% objective-c compatible by just adding
@objc
before the variable.Implementation of the code behind this:
Settings bundle values are live in the
UserDefaults
so you can use a customPropertyWrapper
for it. The following wrapper will work for anyUserDefault
value, including all values of theSettingsBundle
.Property wrapper
The root and the children
You should pass the following enum for the root and the child
plist
s:Find and set defaults if needed.
This wrapper finds the default value of the bundle keys with this function:
This wrapper can store/restore any kind of codable into/from the user defaults including all Swift standard data types that are already conformed to the codable.
Also, you can find a similar but with way less code version for accessing any key-value from any user default, you can take a look at this answer here
试试这个
注册设置捆绑包的默认值
在检索设置捆绑包值同步数据之前
try this
To register Default Values of Setting bundles
Before retrieving setting bundles values synchronize data