sunshinejr / SwiftyUserDefaults

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

Improve scalability #201

Closed Z-JaDe closed 5 years ago

sunshinejr-bot commented 5 years ago
Warnings
:warning: Big PR, try to keep changes smaller if you can
:warning: Any changes to library code should be reflected in the Changelog.
:warning: Only one of either the podspec or SPM package was changed. This might be unintentional – double check.

Generated by :no_entry_sign: Danger Swift against b08e13c961d50b62200ef120728198e9aae2a226

Z-JaDe commented 5 years ago

Protocol-oriented can make projects more scalable and better optimized.

Z-JaDe commented 5 years ago

Class is created inside the heap, but Struct can be optimized into the stack.

Z-JaDe commented 5 years ago

When change DefaultsKey class to struct and change DefaultsKeys class to protocol, The method of using static properties no longer applies, but fortunately, the computed property can return a Struct or Enum, which directly completes the overhead in the stack.

extension DefaultsKeys {
    public static var yourKey: DefaultsKey<Bool> {
        return .init("yourKey", defaultValue: false)
    }
}
sunshinejr commented 5 years ago

@Z-JaDe hey, just wanted to say that this is awesome. I'm pretty sure I want both this & #199 in the next release. In that case, would you create a new PR that bases on top of #199 (still don't close this one as there might be a slight chance #199 won't get in)? I have yet to review it but it would really help me get both of this merged as soon as possible.

Thanks again, seems like it might be a really good improvement if we can make it work with the DefaultsKeyStore.

Z-JaDe commented 5 years ago

@sunshinejr hey, Originally thought that #199 is just a dynamicMemberLookup extension. When I looked at the documentation I found this to be a new feature of swift 5.1. However, api is still uncertain. This feature of swift5.1 is so cool, looking forward to...

Defaults.launchCount = 1
Defaults.remove.launchCount()
Defaults.hasKey.launchCount

The new PR is in #203

sunshinejr commented 5 years ago

Awesome @Z-JaDe. Thanks a lot for doing this. Gonna review it as soon as possible, but this looks exciting!