Reputation: 2305
In the 'old' Swift, I used to register my defaults loaded from a .plist file like this...
let prefs = NSBundle.mainBundle().pathForResource("UserDefaults", ofType: "plist");
let dict = NSDictionary(contentsOfFile: prefs!);
let defaults:NSDictionary! = dict?.valueForKey("defaults") as! NSDictionary;
NSUserDefaults.standardUserDefaults().registerDefaults(defaults);
NSUserDefaults.standardUserDefaults().synchronize();
Under Swift 1.2, I get the error...
Cannot invoke 'registerDefaults' with an argument list of type '(NSDictionary!)'
So, something has changed and Swift no-longer accepts an NSDictionary as a parameter to registerDefaults.
So, how can I convert my NSDictionary to a Dictionary object, given that they're no-longer interchangeable?
Upvotes: 2
Views: 889
Reputation: 114875
You need to provide a type cast from NSDictionary
to a [NSObject:AnyObject]
-
use
let prefs = NSBundle.mainBundle().pathForResource("UserDefaults", ofType: "plist");
let dict = NSDictionary(contentsOfFile: prefs!);
let defaults:NSDictionary! = dict?.valueForKey("defaults") as! NSDictionary
NSUserDefaults.standardUserDefaults().registerDefaults(defaults as [NSObject : AnyObject]);
NSUserDefaults.standardUserDefaults().synchronize();
Upvotes: 5
Reputation: 9346
The user defaults can store NSDictionary objects. These are mapped to Swift as [NSObject : AnyObject]:
Try this:
NSUserDefaults.standardUserDefaults().setObject(defaults, forKey: "defaults")
var isOk = NSUserDefaults.standardUserDefaults().synchronize();
Upvotes: 0