SwiftyUserDefaults icon indicating copy to clipboard operation
SwiftyUserDefaults copied to clipboard

Registering defaults while setting DefaultsKey

Open kaunteya opened this issue 10 years ago • 6 comments

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

kaunteya avatar Oct 29 '15 19:10 kaunteya

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?

radex avatar Oct 29 '15 19:10 radex

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.

kaunteya avatar Oct 30 '15 04:10 kaunteya

this feature would be great.

thellimist avatar Oct 31 '15 22:10 thellimist

:+1:

efremidze avatar Jan 06 '16 10:01 efremidze

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: capturfiles_296

basememara avatar Mar 03 '16 21:03 basememara

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

DivineDominion avatar Nov 26 '16 09:11 DivineDominion