Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

How to change my custom settings from code?

This is my Root.plist file:

enter image description here

And this is visual presentation of that bundle: enter image description here

All I need to do is change the value of Latest clean to appropriate date. I do it in code:

let PBOOrdersLatestCleanKey = "PBOOrdersLatestCleanKey"
let defaults = [PBOOrdersLatestCleanKey: PBODateFormatter(type: .DateAndTimeSimple).stringFromDate(NSDate())]      

NSUserDefaults.standardUserDefaults().registerDefaults(defaults)
NSUserDefaults.standardUserDefaults().synchronize()

But it is now working. The settings are still the same. Why?

Upvotes: 0

Views: 77

Answers (1)

Mustafa Ibrahim
Mustafa Ibrahim

Reputation: 1120

The problem is you didn't set the object to the specific key. You just registered the defaults but for undefined key. Just try

NSUserDefaults.standardUserDefaults().setObject(PBODateFormatter(type: .DateAndTimeSimple).stringFromDate(NSDate()), forKey:PBOOrdersLatestCleanKey)
NSUserDefaults.standardUserDefaults().synchronize()

Good luck

Upvotes: 1

Related Questions