I am attempting to register a Standard UserDefault value in AppDelegate but getting nil when I try to get the value out in a ViewController. I did this all of time in Obj C. What am I missing with the Swift code here? Thanks
AppDelegate
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
setupAppearance()
// register default values for userDefaults
UserDefaults.standard.register(defaults: [
"selectedUUID": "0"
])
UserDefaults.standard.synchronize(). // is this still needed?
return true
}
ViewController
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Get the uuid that was registered in AppDelegate
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var defaults: UserDefaults = UserDefaults.standard
var selectedUUID = defaults.string(forKey: "selectedUUID")
print(selectedUUID!) // selectedUUID is nil, why? }