nmdias / DefaultsKit

Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS
MIT License
1.43k stars 95 forks source link

add a protocol DefaultsAutoProperty, its instructions and tests #12

Closed metasmile closed 6 years ago

metasmile commented 6 years ago

Please confirm and thanks for making your very useful DefaultsKit.

nmdias commented 6 years ago

Hi @metasmile,

Thank you for contributing 👍 Could you explain what an AutoProperty is? And what's it for?

Thanks

edit: scratch that question, there a README.md update in there 🤔

metasmile commented 6 years ago

Hi @nmdias There's no special reason. Just for shorthand senario in devs point of view.

extension Defaults: DefaultsAutoProperty {
    public var autoStringProperty: String? {  set{ set(newValue) } get{ return get() } }
}

So autoStringProperty will be guaranteed.

metasmile commented 6 years ago

And naturally, we can use them with the following style

Defaults.shared.autoStringProperty = "new value will persist"

Rendered readme is here : https://github.com/metasmile/DefaultsKit

rudedogg commented 6 years ago

Just want to chime in and say I'd also like a way to provide a default value/get a non-optional from Defaults().get(). I've only glanced at DefaultsKit, but this does seem like a big change and a different API/usage (via extensions on Defaults per the readme in this PR).

metasmile commented 6 years ago

@rudedogg This is only a utility protocol/extension. Although I'm usefully using it in my project when I register and manage properties and default value/scheme safely, but yes, your opinion may be right. Anyway for reference, this is a way to define non-optional.

var nonOptionalProperty: Int {
        set { set(newValue) } 
        get { return get(or: 5) }  // 5 is default value if "nonOptionalProperty" is nil.
 }