@@ -50,7 +50,7 @@ public struct SmartHexColor: PropertyWrapperable {
5050 }
5151
5252
53- private var encodeHexFormat : HexFormat ?
53+ var encodeHexFormat : HexFormat ?
5454
5555 public init ( wrappedValue: ColorObject ? , encodeHexFormat: HexFormat ? = nil ) {
5656 self . wrappedValue = wrappedValue
@@ -63,6 +63,14 @@ extension SmartHexColor: Codable {
6363 let container = try decoder. singleValueContainer ( )
6464 let hexString = try container. decode ( String . self)
6565
66+
67+ guard let impl = decoder as? JSONDecoderImpl else {
68+ throw DecodingError . dataCorruptedError (
69+ in: container,
70+ debugDescription: " Cannot decode SmartHexColor from ' \( hexString) '. Supported formats: HexFormat. "
71+ )
72+ }
73+
6674 guard
6775 let format = SmartHexColor . HexFormat. format ( for: hexString) ,
6876 let color = SmartHexColor . toColor ( from: hexString, format: format)
@@ -72,11 +80,19 @@ extension SmartHexColor: Codable {
7280 debugDescription: " Cannot decode SmartHexColor from ' \( hexString) '. Supported formats: HexFormat. "
7381 )
7482 }
83+
84+ self . wrappedValue = color
7585
76- if encodeHexFormat == nil {
77- self . encodeHexFormat = format
86+
87+ /**
88+ * 虽然初始化赋值时候`public init(wrappedValue: ColorObject?, encodeHexFormat: HexFormat? = nil)` 提供了 `encodeHexFormat`,但是在 `encode` 解析时重新初始化了对象导致赋值的 `encodeHexFormat` 没了。
89+ * 通过缓存 `Cache` 获取使用者设置的该值。
90+ * 再赋值到新对象的属性上。
91+ */
92+ if let arr = impl. codingPath. removeFromEnd ( 1 ) ,
93+ let hexColor: SmartHexColor = try ? impl. cache. initialValue ( forKey: impl. codingPath. last, codingPath: arr) {
94+ self . encodeHexFormat = hexColor. encodeHexFormat
7895 }
79- self . wrappedValue = color
8096 }
8197
8298
0 commit comments