Skip to content

Commit 2f1e3ff

Browse files
author
Mccc
committed
优化SmartFlat
1 parent 6283163 commit 2f1e3ff

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable (4.3.7)
11+
- SmartCodable (4.3.7-beta)
1212
- SnapKit (5.6.0)
1313

1414
DEPENDENCIES:
@@ -39,7 +39,7 @@ SPEC CHECKSUMS:
3939
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4040
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4141
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
42-
SmartCodable: 56babd0a912588ce299f2d0799633126f4c427c2
42+
SmartCodable: a20b3bde2d0475de940386a902030f60804a1f66
4343
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4444

4545
PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f

Example/Pods/Local Podspecs/SmartCodable.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SmartCodable/TestViewController.swift

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,35 @@ class TestViewController: BaseViewController {
3535
override func viewDidLoad() {
3636
super.viewDidLoad()
3737

38-
39-
}
40-
41-
42-
43-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
44-
4538
let dict: [String: Any] =
4639
[
47-
"data" : [
48-
"enableJson" : "111",
49-
],
40+
"location" : "suzhou",
41+
"name": "Mccc",
42+
"age": 18
5043
]
51-
52-
5344

54-
let model = ResultModel.deserialize(from: dict)
55-
print(model?.data?.enableJson)
45+
guard let model = Model.deserialize(from: dict) else { return }
5646

47+
let transDict = model.toDictionary() ?? [:]
48+
print(transDict)
5749
}
50+
51+
52+
53+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
5854

59-
public struct ResultModel: SmartCodable {
60-
public init() {}
6155

62-
// //后台返回字段
63-
@SmartAny
64-
public var data: NotifyEnableModel?
6556
}
6657

67-
68-
struct NotifyEnableModel: SmartCodable {
69-
// required init() {}
70-
// 服务器返回 Json
71-
var enableJson: String = ""
58+
struct Model: SmartCodable {
59+
var location: String = ""
7260

73-
mutating func didFinishMapping() {
74-
enableJson = "2222"
75-
print("NotifyEnableModel_didFinishMapping")
76-
}
61+
@SmartFlat
62+
var model: BaseModel?
63+
}
64+
65+
struct BaseModel: SmartCodable {
66+
var name: String = ""
67+
var age: Int = 0
7768
}
7869
}

SmartCodable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Pod::Spec.new do |s|
1414
s.name = 'SmartCodable'
15-
s.version = '4.3.7'
15+
s.version = '4.3.7-beta'
1616
s.summary = 'Swift数据解析库'
1717

1818
s.homepage = 'https://github.com/intsig171'

SmartCodable/Classes/JSONEncoder/Impl/_SpecialTreatmentEncoder.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ extension _SpecialTreatmentEncoder {
7272
let encoder = self.getEncoder(for: additionalKey)
7373
try encodable.encode(to: encoder)
7474

75+
// 如果是被SmartFlat修饰的,需要向上层encode,让数据恢复原样。
76+
if encodable is FlatType {
77+
if let object = encoder.value?.object {
78+
for (key, value) in object {
79+
self.impl.object?.set(value, for: key)
80+
}
81+
return nil
82+
}
83+
}
84+
7585
impl.cache.removeSnapshot(for: E.self)
76-
7786
return encoder.value
7887
}
7988
}

0 commit comments

Comments
 (0)