Registering defaults while setting DefaultsKey
Can we consider registering default value to every static key while initialising
For eg:
let colorKey = DefaultsKey<String, UIColor>(["color": UIColor.grayColor()])
Where key named "color" would be set and default value would be set to gray using register user defaults
Having statically-defined default values would be very nice indeed. I need to consider more closely if there's a nice way to achieve that given current Swift limitations (no generic subscripts, no extending generic types, etc...)
Could you explain this bit?
DefaultsKey<String, UIColor>(["color": UIColor.grayColor()])
Why does it have two type params and a dictionary — I don't get it?
It does not have any syntactical significance. I coined it without giving much thought. Yes, the dictionary does not makes sense. Tuple could have been better I guess, but might have limitations.
this feature would be great.
:+1:
Check out my pull request (#81) and see if that works for everyone (including @MaxHasADHD too). It adds an extended version of registerDefaults. I updated the readme docs and added a ton of unit tests for it. You'd use it like this:
let Defaults = NSUserDefaults.registerDefaults("Settings.plist")
let Defaults2 = NSUserDefaults.registerDefaults("Settings.plist", suiteName: "io.radex.SwiftyUserDefaults")
let Defaults3 = NSUserDefaults.registerDefaults(bundleName: "Settings")
let key1 = DefaultsKey<String>("MyString1")
Defaults[key1] == "My string value 1." // true
And the plist would look something like this:

This looks cool. Any issues so far, or would this be merge-able?