SwiftyUserDefaults icon indicating copy to clipboard operation
SwiftyUserDefaults copied to clipboard

NSUserDefaults registerDefaults with Swifty User Defaults

Open MaxHasADHD opened this issue 9 years ago • 4 comments

Hashable and Equatable protocols need to be added so Defaults.registerDefaults([:]) can work

MaxHasADHD avatar Feb 18 '16 06:02 MaxHasADHD

@MaxHasADHD I don't understand what you're trying to say — can you elaborate?

radex avatar Feb 29 '16 13:02 radex

NSUserDefaults has a function named registerDefaults that is faster to use on launch to set initial defaults without checking if it exists and setting it. I would like a way to use Swifty Defaults with this function, currently I have to use the full default key like Defaults.registerDefaults([ DefaultsKeys.AskedToEnableNotificationsKey._key: false]).

So really, I just want a way so its cleaner and I can just use the short syntax without the DefaultsKeys and ._key. To use a DefaultKey in a dictionary though, it needs to conform to Hashable and Equatable. I have tried to add this myself but I can't get it to be generic, so I can have all bools or all ints registered but not any type.

MaxHasADHD avatar Feb 29 '16 14:02 MaxHasADHD

OK, got it. #59 was proposing an alternative to using registerDefaults, instead, embedding the default value in the DefaultsKey definition.

The latter definitely seems cleaner, but registerDefaults has the advantage of working across NSUserDefaults, whether or not you actually use SwiftyUserDefaults and those key values.

radex avatar Feb 29 '16 14:02 radex

Yeah, just ran into this actually. I upgraded from SwiftyUserDefaults 3 to 5 and removed registerDefaults before realizing an internal framework where I'm using the standard API now has no default values. So I'd still love some solution for calling registerDefaults

MaxHasADHD avatar May 17 '20 14:05 MaxHasADHD