Skip to content

Commit 5efdf4a

Browse files
committed
修复SmartHexColor encode bug
1 parent dacb7f7 commit 5efdf4a

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

Example/SmartCodable/Smart/1.Introduce(使用介绍)/Introduce_9ViewController.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ class Introduce_9ViewController: BaseCompatibilityViewController {
2222

2323
guard let model = Model.deserialize(from: dict) else { return }
2424
print(model.color as Any)
25+
26+
let trans = model.toDictionary()
27+
print(trans)
2528
}
2629
}
2730

28-
2931
extension Introduce_9ViewController {
30-
3132
struct Model: SmartCodable {
32-
@SmartHexColor
33+
@SmartHexColor(encodeHexFormat: .rrggbbaa(.hash))
3334
var color: UIColor? = .white
3435
}
3536
}

Sources/SmartCodable/Core/PropertyWrapper/SmartHexColor.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Sources/SmartCodable/Core/Sentinel/LogItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension LogItem {
9090
}
9191

9292
extension Array {
93-
fileprivate func removeFromEnd(_ count: Int) -> [Element]? {
93+
func removeFromEnd(_ count: Int) -> [Element]? {
9494
guard count >= 0 else { return nil }
9595
let endIndex = self.count - count
9696
guard endIndex >= 0 else { return nil }

0 commit comments

Comments
 (0)