@@ -15,7 +15,7 @@ import CleanJSON
1515/** 测试内容项
1616 1. 默认值的使用是否正常
1717 2. mappingForValue是否正常。
18- 3.
18+ 3.
1919 */
2020
2121
@@ -25,67 +25,33 @@ class Test3ViewController: BaseViewController {
2525
2626 override func viewDidLoad( ) {
2727 super. viewDidLoad ( )
28-
29-
3028
3129 let dict1 : [ String : Any ] = [
32- " age " : 10 ,
33- " name " : " Mccc " ,
34- " location " : [
35- " province " : " Jiang zhou " ,
36- " city " : " Su zhou " ,
30+ " code " : " 10000 " ,
31+ " msg " : " 成功 " ,
32+ " data " : [
33+ " guideSvga " : " guideSvga " ,
34+ " guideOnevga " : " guideOnevga " ,
35+ " loadingSvga " : " loadingSvga " ,
36+ " loadingSvgaBackgroundColor " : " loadingSvgaBackgroundColor " ,
3737 ]
38-
3938 ]
4039
41- if let jsonData = try ? JSONSerialization . data ( withJSONObject: dict1, options: [ ] ) {
42- // Successfully converted Dictionary to Data
43- print ( " JSON Data: " , jsonData)
44-
45- do {
46- let obj = try JSONDecoder ( ) . decode ( Model . self, from: jsonData)
47- print ( " obj = " , obj)
48-
49- } catch {
50- print ( " error = " , error)
51- }
52-
53- // If you want to convert it back to a String for debugging purposes
54- if let jsonString = String ( data: jsonData, encoding: . utf8) {
55- print ( " JSON String: " , jsonString)
56- }
57- }
58-
59- if let model = Model . deserialize ( from: dict1) {
60- smartPrint ( value: model)
61- print ( " \n " )
62- let dict = model. toDictionary ( ) ?? [ : ]
63- print ( dict)
64- }
40+ guard let model = ResponseData< HomeListModel> . deserialize( from: dict1) else { return }
41+ print ( model)
6542 }
66- }
67-
68-
69-
70-
71- extension Test3ViewController {
72- struct Model : SmartCodable {
73- var name : String = " "
74- @IgnoredKey
75- var ignore : String = " "
76- @IgnoredKey
77- var ignore2 = " "
78- var age : Int = 0
79- var location : Location ?
43+
44+ struct HomeListModel : SmartCodable {
45+ var guideSvga = " "
46+ var guideOnevga = " "
47+ var loadingSvga = " "
48+ var loadingSvgaBackgroundColor = " "
8049 }
8150
82- struct Location : SmartCodable {
83- var province : String = " "
84-
85- // 忽略解析
86- @IgnoredKey
87- var city : String = " area123 "
51+ struct ResponseData < T> : SmartCodable where T: SmartCodable {
52+ var code = " "
53+ var msg = " "
54+ var data : T ?
8855 }
8956}
9057
91-
0 commit comments