Skip to content

Commit ff6f130

Browse files
author
Mccc
committed
优化继承的默认值使用
1 parent f9942fc commit ff6f130

2 files changed

Lines changed: 61 additions & 53 deletions

File tree

Example/SmartCodable/TestViewController.swift

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,53 +34,58 @@ class TestViewController: BaseViewController {
3434

3535
override func viewDidLoad() {
3636
super.viewDidLoad()
37-
let jsonString = """
38-
[
39-
{
40-
"enjoyCount": 1,
41-
"commentCount": 1,
42-
},
43-
{
44-
"enjoyCount": 2,
45-
"commentCount": 2,
46-
},
47-
{
48-
"enjoyCount": 2,
49-
"commentCount": 2,
37+
38+
let dict: [String: Any] = [
39+
"size": 1,
40+
"id": 2,
41+
"name": "Mccc"
42+
]
43+
44+
guard let model = C.deserialize(from: dict) else { return }
45+
46+
print(model.size)
47+
print(model.id)
48+
print(model.name)
49+
}
5050
}
51-
]
51+
52+
class A: SmartCodable {
53+
var size: Int?
54+
@IgnoredKey
55+
var attr: NSMutableAttributedString?
56+
required init() { }
57+
}
58+
59+
class B: A {
60+
var name: String?
5261

53-
"""
54-
guard let models = [RecommendModel].deserialize(from: jsonString) else {
55-
return
56-
}
57-
print(models)
58-
print("\n")
62+
enum CodingKeys: CodingKey {
63+
case name
64+
}
65+
required init(from decoder: any Decoder) throws {
66+
try super.init(from: decoder)
5967

60-
let uniqueRecommends = Array(Set(models))
61-
print(uniqueRecommends)
68+
let container = try decoder.container(keyedBy: CodingKeys.self)
69+
self.name = try container.decodeIfPresent(String.self, forKey: .name)
6270
}
63-
64-
struct RecommendModel: SmartCodable, Equatable, Hashable {
71+
required init() {
72+
super.init()
73+
}
74+
}
6575

66-
/// 点赞数
67-
var enjoyCount: Int = 0
68-
/// 评论数
69-
var commentCount: Int = 0
70-
71-
@IgnoredKey
72-
var priceText: NSAttributedString? // 价格富文本
73-
74-
75-
static func == (lhs: RecommendModel, rhs: RecommendModel) -> Bool {
76-
return true
77-
}
76+
77+
class C: B {
78+
var id: Int?
79+
enum CodingKeys: CodingKey {
80+
case id
81+
}
82+
required init(from decoder: any Decoder) throws {
83+
try super.init(from: decoder)
7884

79-
// 手动实现 Hashable 协议
80-
func hash(into hasher: inout Hasher) {
81-
hasher.combine(enjoyCount)
82-
hasher.combine(commentCount)
83-
}
85+
let container = try decoder.container(keyedBy: CodingKeys.self)
86+
self.id = try container.decodeIfPresent(Int.self, forKey: .id)
87+
}
88+
required init() {
89+
super.init()
8490
}
85-
8691
}

SmartCodable/Classes/JSONDecoder/Decoder/DecodingCache.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@ class DecodingCache {
2626

2727
/// Cache the initial state of a Decodable object.
2828
func cacheInitialState<T: Decodable>(for type: T.Type) {
29-
30-
3129
if let object = type as? SmartDecodable.Type {
32-
3330
decodedType = object
34-
3531
var snapshot = Snapshot()
3632

3733
let instance = object.init()
38-
let mirror = Mirror(reflecting: instance)
39-
mirror.children.forEach { child in
40-
if let key = child.label {
41-
snapshot.initialValues[key] = child.value
34+
// 递归处理所有的 superclassMirror
35+
func captureInitialValues(from mirror: Mirror) {
36+
mirror.children.forEach { child in
37+
if let key = child.label {
38+
snapshot.initialValues[key] = child.value
39+
}
40+
}
41+
if let superclassMirror = mirror.superclassMirror {
42+
captureInitialValues(from: superclassMirror)
4243
}
4344
}
44-
snapshot.typeName = "\(type)"
45+
// 获取当前类和所有父类的属性值
46+
let mirror = Mirror(reflecting: instance)
47+
captureInitialValues(from: mirror)
48+
4549

50+
snapshot.typeName = "\(type)"
4651
snapshot.transformers = object.mappingForValue() ?? []
47-
4852
snapshots.append(snapshot)
4953
}
5054
}
@@ -86,7 +90,6 @@ class DecodingCache {
8690
}
8791
}
8892
}
89-
9093
return nil
9194
}
9295

0 commit comments

Comments
 (0)