从 Swift 3 中的用户默认值检索字符串值

2024-01-07

我有一个方法可以设置String值在UserDefaults,但是当我尝试检索 a 中的值时print声明,我得到nil。谁能看到我做错了什么吗?

    static func setPreferredUnitsOfMeasurementSystem(_ system: UnitsOfMeasurementSystem) {
        // system.rawValue is of type String
        UserDefaults.standard().set(system.rawValue, forKey: unitsOfMeasurementSystemKey)
        print(UserDefaults.standard().string(forKey: unitsOfMeasurementSystemKey))
    }

我刚刚遇到了同样的问题。看起来像一个模拟器错误。重新启动 Mac 并运行 iOS 10 SIM 似乎可以解决此问题。看使用 xcode 8 将字典保存在 swift 3 的 userdefaults 中 https://stackoverflow.com/questions/37825630/save-dictionary-in-userdefaults-in-swift-3-with-xcode-8.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Swift 3 中的用户默认值检索字符串值 的相关文章

随机推荐