Retrieving String Value from User Defaults in Swift 3

谁说我不能喝 提交于 2019-12-10 18:48:03

问题


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))
    }

回答1:


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.



来源:https://stackoverflow.com/questions/38135930/retrieving-string-value-from-user-defaults-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!