From bac1b5949afd16ab17b85673a1d005838e7746af Mon Sep 17 00:00:00 2001 From: liyang <18616562401@163.com> Date: Fri, 24 Dec 2021 11:30:57 +0800 Subject: [PATCH 1/3] add initialize from key string method to PropertyWrapper --- Sources/PropertyWrappers.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sources/PropertyWrappers.swift b/Sources/PropertyWrappers.swift index 585038f1..fba4277e 100644 --- a/Sources/PropertyWrappers.swift +++ b/Sources/PropertyWrappers.swift @@ -80,8 +80,25 @@ public final class SwiftyUserDefault where T.T == T { } } + public init(key: String, defaultValue value: T, options: SwiftyUserDefaultOptions = []) { + self.key = DefaultsKey(key, defaultValue: value) + self.options = options + + if options.contains(.observed) { + observation = Defaults.observe(self.key) { [weak self] update in + self?._value = update.newValue + } + } + } deinit { observation?.dispose() } } +extension SwiftyUserDefault where T: OptionalType, T.Wrapped: DefaultsSerializable { + public convenience init(key: String, options: SwiftyUserDefaultOptions = []) { + self.init(key: key, defaultValue: T.__swifty_empty, options: options) + } +} + + #endif From ac017ece3511d5bcaa6562170fbb7d3baca03813 Mon Sep 17 00:00:00 2001 From: liyang <18616562401@163.com> Date: Fri, 9 Dec 2022 15:10:04 +0800 Subject: [PATCH 2/3] 1. xxx --- Sources/BuiltIns.swift | 4 ++-- Sources/PropertyWrappers.swift | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/BuiltIns.swift b/Sources/BuiltIns.swift index 8a727265..42e4457a 100644 --- a/Sources/BuiltIns.swift +++ b/Sources/BuiltIns.swift @@ -93,6 +93,6 @@ extension Optional: DefaultsSerializable where Wrapped: DefaultsSerializable { public typealias Bridge = DefaultsOptionalBridge public typealias ArrayBridge = DefaultsOptionalBridge - public static var _defaults: DefaultsOptionalBridge { return DefaultsOptionalBridge(bridge: Wrapped._defaults) } - public static var _defaultsArray: DefaultsOptionalBridge { return DefaultsOptionalBridge(bridge: Wrapped._defaultsArray) } + public static var _defaults: Bridge { return Bridge(bridge: Wrapped._defaults) } + public static var _defaultsArray: ArrayBridge { return ArrayBridge(bridge: Wrapped._defaultsArray) } } diff --git a/Sources/PropertyWrappers.swift b/Sources/PropertyWrappers.swift index fba4277e..283d489d 100644 --- a/Sources/PropertyWrappers.swift +++ b/Sources/PropertyWrappers.swift @@ -44,7 +44,10 @@ public final class SwiftyUserDefault where T.T == T { public var wrappedValue: T { get { if options.contains(.cached) { - return _value ?? Defaults[key: key] + if let v = _value { return v } + let v = Defaults[key: key] + _value = v + return v } else { return Defaults[key: key] } From 05270959c67d10b4360d5e09b409ca60930d5c6e Mon Sep 17 00:00:00 2001 From: liyang <18616562401@163.com> Date: Tue, 19 Sep 2023 16:49:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?1.=20=E7=AE=80=E5=8C=96PropertyWrapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/BuiltIns.swift | 4 ++-- Sources/DefaultsObserver.swift | 2 +- Sources/PropertyWrappers.swift | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Sources/BuiltIns.swift b/Sources/BuiltIns.swift index 42e4457a..8a727265 100644 --- a/Sources/BuiltIns.swift +++ b/Sources/BuiltIns.swift @@ -93,6 +93,6 @@ extension Optional: DefaultsSerializable where Wrapped: DefaultsSerializable { public typealias Bridge = DefaultsOptionalBridge public typealias ArrayBridge = DefaultsOptionalBridge - public static var _defaults: Bridge { return Bridge(bridge: Wrapped._defaults) } - public static var _defaultsArray: ArrayBridge { return ArrayBridge(bridge: Wrapped._defaultsArray) } + public static var _defaults: DefaultsOptionalBridge { return DefaultsOptionalBridge(bridge: Wrapped._defaults) } + public static var _defaultsArray: DefaultsOptionalBridge { return DefaultsOptionalBridge(bridge: Wrapped._defaultsArray) } } diff --git a/Sources/DefaultsObserver.swift b/Sources/DefaultsObserver.swift index 80a2d1e5..207f97ee 100644 --- a/Sources/DefaultsObserver.swift +++ b/Sources/DefaultsObserver.swift @@ -51,7 +51,7 @@ public final class DefaultsObserver: NSObject, Defaults private static func deserialize(_ value: Any?, for key: DefaultsKey) -> T.T? where T.T == T { guard let value = value else { return nil } - let deserialized = T._defaults.deserialize(value) + let deserialized = T._defaults.deserialize(value) let ret: T.T? if key.isOptional, let _deserialized = deserialized, let __deserialized = _deserialized as? OptionalTypeCheck, !__deserialized.isNil { diff --git a/Sources/PropertyWrappers.swift b/Sources/PropertyWrappers.swift index 283d489d..2eae0825 100644 --- a/Sources/PropertyWrappers.swift +++ b/Sources/PropertyWrappers.swift @@ -35,6 +35,33 @@ public struct SwiftyUserDefaultOptions: OptionSet { } } + +@propertyWrapper +public struct SwiftyDefaults where T.T == T { + public let key: DefaultsKey + private var _value: T.T + public var wrappedValue: T.T { + get { _value } + set { + _value = newValue + Defaults[key: key] = newValue + } + } + public init(keyPath: KeyPath>) { + self.key = Defaults.keyStore[keyPath: keyPath] + self._value = Defaults[key: self.key] + } + public init(key: String, defaultValue value: T) { + self.key = DefaultsKey(key, defaultValue: value) + self._value = Defaults[key: self.key] + } +} +extension SwiftyDefaults where T: OptionalType, T.Wrapped: DefaultsSerializable { + public init(key: String) { + self.init(key: key, defaultValue: T.__swifty_empty) + } +} + @propertyWrapper public final class SwiftyUserDefault where T.T == T {