在 Swift 3 中从用户默认值中检索字符串值 [英] Retrieving String Value from User Defaults in Swift 3

查看:47
本文介绍了在 Swift 3 中从用户默认值中检索字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method that sets a String value in UserDefaults, but when I try to retrieve the value in a print statement, I get nil. Can anyone see what I am doing incorrectly?

    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 中的用户默认值中保存字典.

I just ran into the same issue. Seems like a simulator bug. Restarting your Mac and running JUST the iOS 10 sim seems to fix it. See Save dictionary in userdefaults in swift 3 with xcode 8.

这篇关于在 Swift 3 中从用户默认值中检索字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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