3

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))
    }
Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49

1 Answers1

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.

Community
  • 1
  • 1
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172