【问题标题】:Retrieving string from UserDefaults not working从 UserDefaults 中检索字符串不起作用
【发布时间】:2018-10-10 08:52:22
【问题描述】:

我在 Swift 中创建了一个简单的应用程序,其中包含 WebViewControl。感谢SettingsBundle,用户可以指定一个URL,然后在WebView 中打开。 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>PSTextFieldSpecifier</string>
            <key>Title</key>
            <string>URL</string>
            <key>Key</key>
            <string>app_url</string>
            <key>DefaultValue</key>
            <string>https://myurl.com</string>
            <key>IsSecure</key>
            <false/>
            <key>KeyboardType</key>
            <string>URL</string>
            <key>AutocapitalizationType</key>
            <string>None</string>
            <key>AutocorrectionType</key>
            <string>No</string>
        </dict>
    </array>
</dict>
</plist>

这很好用。但是,当尝试在 Swift 中检索 app_url 时,我遇到了问题。

override func viewDidLoad() {
    super.viewDidLoad()

    registerSettingsBundle()

    print("retrieving key")
    if let appUrl = UserDefaults.standard.string(forKey: "app_url") {
        print("got it " + appUrl)
    }
}

func registerSettingsBundle(){
    let appDefaults = [String:AnyObject]()
    UserDefaults.standard.register(defaults: appDefaults)
}

这会将retrieving key 打印到控制台,但不会打印第二个打印语句。

它在不久前还可以工作,但最近的一次更新破坏了它。我究竟做错了什么? API是否以某种方式发生了变化?我读过the documentation from Apple on UserDefaults,但到目前为止它对我没有帮助。

编辑 marked as duplicate:用户能够使用 iOS 中的设置应用程序更改 app_url 至关重要。仅将 plist 文件作为 dict 读取是不够的。请原谅,这是我第一次尝试 iOS 应用。

【问题讨论】:

  • URL 如何从您的.plist 文件中获取到UserDefaults
  • Root.plist != UserDefaults。从 plist 中读取:stackoverflow.com/questions/24045570/…
  • @DávidPásztor 请看我的编辑。
  • @dislick 你为什么要注册应用默认值,因为你可以从 app bundle 的 sharedDefaults 中获取值。
  • @vadian 谢谢。我完全删除了 registerSettingsBundle 函数,但是我仍然无法检索 URL。还有其他想法吗?

标签: swift userdefaults


【解决方案1】:

我重新打开了这个问题,因为它不是重复的。 settings plist 无论如何都不应该在代码中打开。

问题是registerSettingsBundle。调用此方法后,默认字典为空,检索任何数据都将失败。不要调用registerSettingsBundle,因为您在设置包中声明了默认值。

registerSettingsBundle()

因为类型是 URL,所以使用url(forKey

【讨论】:

    【解决方案2】:

    您正在尝试从plist 获取数据,因此您需要这样做

    if let path = Bundle.main.path(forResource: "SettingsBundle", ofType: "plist") {
    
        //plist contain root as Array
        if let arrayData = NSArray(contentsOfFile: path) as? [[String: Any]] {
    
        }
    
        //Or plist contain root as Dictionary
        if let dictData = NSDictionary(contentsOfFile: path) as? [String: Any] {
    
        }
    }
    

    如果要更改 plist 数据,请使用:

    var dictionaryPath = NSBundle.mainBundle().pathForResource("SettingsBundle", ofType: "plist")
    
    var dictionary = NSMutableDictionary(contentsOfFile: dictionaryPath!)
    
    var array = dictionary?.objectForKey("key")? as NSMutableArray
    
    //this is a label I have called player1Name
    array[0] = ""
    
    playersDictionary?.writeToFile(dictionaryPath!, atomically: true)
    

    【讨论】:

    • 谢谢。但我想这不会允许用户更改 Settings.app 中的 URL?
    • @dislick:您可以更改值
    • 请不要在 Swift 中建议 NS(Mutable)... 集合类型。始终使用 PropertyListSerialization 来读取 plist 文件。而且你永远不会得到一个类型转换为NSMutableArray 的可变数组。
    • @vadian:谢谢,是的。我会记住的
    猜你喜欢
    • 2018-12-03
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    相关资源
    最近更新 更多