问题
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