0

Can somebody tell me, why I get nil, if I query

let stage = UserDefaults.standard.dictionary(forKey: "stageSelection")

my UserDefaults? There is a default vaule and I also changed the value in the settings. The settings screen is working fine - my problem is that I cannot query the selected value.

enter image description here

enter image description here

netshark1000
  • 7,245
  • 9
  • 59
  • 116

1 Answers1

0

You're querying for a dictionary value, but there's only a String stored in UserDefaults.

Use let stage = UserDefaults.standard.string(forKey: "stageSelection")

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • let stage = UserDefaults.standard.string(forKey: "stageSelection") still returns nil – netshark1000 Jul 17 '18 at 14:28
  • That's to be expected until you either set a new value, or register a default using `UserDefaults.standard.register(defaults: ["stageSelection": "prod"])`. See also https://stackoverflow.com/questions/9181544/setting-bundle-default-value-wont-set – Gereon Jul 17 '18 at 14:42