设置包值返回nil [英] Settings bundle values returning nil
问题描述
我在我的应用程序中添加了一个设置包,在Xcode中它出现在项目树视图的根目录中。
I added a Settings bundle to my app and in Xcode it appears in the root of my project tree view.
Root.plist
文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Service</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Hostname</string>
<key>Key</key>
<string>service_hostname</string>
<!-- and so on -->
当我在iOS上打开设置应用时,条目显示在底部,我可以显示和编辑我的设置完全正常。
When I open the Settings app on iOS the entry appears at the bottom and I can display and edit my settings perfectly fine.
但是我无法从代码中检索这些值。这是我的代码:
However I cannot retrieve these values from code. Here's my code:
static func loadSettings() {
let ud = NSUserDefaults.standardUserDefaults()
ud.synchronize()
Settings.hostName = ud.stringForKey("service_hostname")
// etc
}
我也试过 ud.objectForKey
和 ud.valueForKey
- 两者都返回 nil
。
I also tried ud.objectForKey
and ud.valueForKey
- both return nil
as well.
设置设置后.hostName
尽管我在设置应用中设置了显式值,但Xcode调试器报告它的值为 nil
。
After setting Settings.hostName
the Xcode debugger reports it has a value of nil
despite me setting an explicit value in the Settings app.
我看到了这个帖子( iPhone应用程序:如何从root.plist获取默认值?)有人发布了一块Objective-C代码,手动加载 Root.plist
文件直接进入 NSMutableDictionary
并调用 NSUserDefaults.standardUserDefaults()。 gisterDefaults
但这似乎是一个黑客攻击(我无法让它在Swift中工作,因为编译器说 stringByAppendingPathComponent
不再存在)
I saw this thread ( iPhone App : How to get default value from root.plist? ) where someone posted a chunk of Objective-C code that manually loads the Root.plist
file directly into an NSMutableDictionary
and calls NSUserDefaults.standardUserDefaults().registerDefaults
but that seems like a hack (and I can't get it to work in Swift because the compiler says that stringByAppendingPathComponent
doesn't exist anymore)
为什么 NSUserDefaults
从设置应用中获取设置?
Why isn't NSUserDefaults
picking up the settings from the Settings app?
推荐答案
显然原因是如果我在 plist
中的设置定义了默认值而用户没有显式设置一个值,然后设置应用程序中显示的值将是 plist
文件中的默认值,但是 NSUserDefaults
API仍将返回 nil
。
Apparently the cause is that if my settings in the plist
have defaults defined and the user has not explicitly set a value, then the value displayed in the Settings app will be the defaults from the plist
file, however the NSUserDefaults
API will still return nil
.
不幸的是,这意味着如果默认值有意义(例如默认值) Web服务地址URI: http://www.example.com
)它必须在我的项目中存在两次:作为中的默认值plist
并在我的程序代码中:
Unfortunately this means that if the default value is meaningful (such as a default web-service address URI: "http://www.example.com
") it must exist twice in my project: as a default in the plist
and in my program code:
Root.plist:
Root.plist:
<dict>
<key>Key</key> <string>mySettingKey</string>
<key>Title</key> <string>Some address</string>
<key>Type</key> <string>PSTextFieldSpecifier</string>
<key>DefaultValue</key> <string>http://www.example.com</string>
<key>IsSecure</key> <false />
<key>KeyboardType</key> <string>Alphabet</string>
</dict>
Program.swift:
Program.swift:
let ud = NSUserDefaults.standardUserDefaults()
ud.synchronize()
var mySettingValue = ud.stringForKey("mySettingKey")
if mySettingValue == nil {
mySettingValue = "http://www.example.com"
}
这令人惊讶。
这篇关于设置包值返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!