sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 366 forks source link

[WIP] use DefaultsKey.defaultValue for registering user defaults #189

Open DivineDominion opened 5 years ago

DivineDominion commented 5 years ago

I'm working on macOS apps and rely on the effect of register(defaults:) a lot. It didn't bother me to register them manually, using the swifty keys in the dictionary like so: DefaultsKeys.myKey._key : 123

But now that the keys require a default value, I'd like to not duplicate using this.

Either make the defaultValue public for compatibility, I figured, or offer a convenient wrapper around the register(defaults:) method. This is a prototype for the latter.

Is this a direction you want to go?

Usage:

extension DefaultsKeys {
    static let foo = DefaultsKey<Bool>("foo", defaultValue: true)
}

Defaults.register(defaultsKeys: [ DefaultsKeys.foo, ... ])

This works thanks to a protocol that type-erases the internals of DefaultsKey<T>:

public protocol DefaultsKeyable {
    var _key: String { get }
    var _defaultValue: Any? { get }
}

It's not that convenient to have to use DefaultsKeys.foo to reference the key. But I think the DefaultsKeys class can become a protocol instead, and the new DefaultsKeyable can inherit from that, and then it should work to write .register(defaultsKeys: [ .foo, ... ])

sunshinejr commented 5 years ago

@DivineDominion Yeah I agree, we should make defaultValue public - no reason to hide it behind our framework if it's user-provided value anyways. Would it make the implementation easier?

Also, about making DefaultsKeys a protocol - this would make it impossible to have stored values in extension, right? We would have to make them all computed?

DivineDominion commented 5 years ago

Also, about making DefaultsKeys a protocol - this would make it impossible to have stored values in extension, right? We would have to make them all computed?

You're right, I totally missed that part.

DivineDominion commented 5 years ago

During field-testing I found there's more work to be done: