diff --git a/Document/README/Usages.md b/Document/README/Usages.md index 4f9e5af..5d759ab 100644 --- a/Document/README/Usages.md +++ b/Document/README/Usages.md @@ -544,19 +544,26 @@ struct SubModel: SmartCodable { ### Update Existing Model(更新现有模型) ``` -var dest = Model(name: "xiaoming", hobby: "football") -let src = Model(name: "dahuang", hobby: "sleep") +struct Model: SmartCodable { + var name: String = "" + var age: Int = 0 +} -SmartUpdater.update(&dest, from: src, keyPath: \.name) -// after this dest will be: -// Model(name: "dahuang", hobby: Optional("football")) -// instead of -// Model(name: "xiaoming", hobby: Optional("football")) -``` +var dic1: [String : Any] = [ + "name": "mccc", + "age": 10 +] +let dic2: [String : Any] = [ + "age": 200 +] -If you need to change more than one at a time(如果你需要同时更改多个) +guard var model = Model.deserialize(from: dic1) else { return } +SmartUpdater.update(&model, from: dic2) + +// now: model is ["name": mccc, "age": 200]. ``` -SmartUpdater.update(&dest, from: src, keyPaths: (\.name, \.hobby)) -``` +It can accommodate any data structure, including nested array structures. + +它可以适应任何数据结构,包括嵌套的数组结构。 diff --git "a/Example/SmartCodable/Smart/1.Introduce(\344\275\277\347\224\250\344\273\213\347\273\215)/Introduce_11ViewController.swift" "b/Example/SmartCodable/Smart/1.Introduce(\344\275\277\347\224\250\344\273\213\347\273\215)/Introduce_11ViewController.swift" index 26fe4be..70954c6 100644 --- "a/Example/SmartCodable/Smart/1.Introduce(\344\275\277\347\224\250\344\273\213\347\273\215)/Introduce_11ViewController.swift" +++ "b/Example/SmartCodable/Smart/1.Introduce(\344\275\277\347\224\250\344\273\213\347\273\215)/Introduce_11ViewController.swift" @@ -14,28 +14,22 @@ class Introduce_11ViewController: BaseCompatibilityViewController { override func viewDidLoad() { super.viewDidLoad() - - var dest = Model(name: "xiaoming", hobby: "football") - let src = Model(name: "dahuang", hobby: "sleep") - -// print(dest) -// SmartUpdater.update(&dest, from: src, keyPath: \.name) -// -// print(dest) -// -// SmartUpdater.update(&dest, from: src, keyPaths: (\.name, \.hobby)) -// -// print(dest) + var dic1: [String : Any] = [ + "name": "mccc", + "age": 10 + ] + let dic2: [String : Any] = [ + "age": 200 + ] + + guard var model = Model.deserialize(from: dic1) else { return } + SmartUpdater.update(&model, from: dic2) + smartPrint(value: model) } -} - - -extension Introduce_11ViewController { - struct Model: SmartCodable { var name: String = "" - var hobby: String? + var age: Int = 0 } } diff --git a/Example/SmartCodable/Test2ViewController.swift b/Example/SmartCodable/Test2ViewController.swift index 0447b66..8e7833f 100644 --- a/Example/SmartCodable/Test2ViewController.swift +++ b/Example/SmartCodable/Test2ViewController.swift @@ -19,39 +19,20 @@ class Test2ViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() - var dic: [String : Any] = [ - "a1": "1111", - "a2": 11111, - "a3": [ - "b1": "2222", - "b2": 2222, - "b3": [1, 2, 3] - ] + var dic1: [String : Any] = [ + "name": "mccc", + "age": 10 ] let dic2: [String : Any] = [ - "a2": 2222, - "a3": [ - "b3": [100, 200, 300] - ] + "age": 200 ] - guard var model = Model.deserialize(from: dic) else { return } - - - let json2 = dic2.bt_toJSONString() - - SmartUpdater.update(&model, from: json2) - smartPrint(value: model) + guard var model = Model.deserialize(from: dic1) else { return } + SmartUpdater.update(&model, from: dic2) } struct Model: SmartCodable { - var a1: String = "" - var a2: Int = 0 - var a3: SubModel = SubModel() - } - - struct SubModel: SmartCodable { - var b1: String = "" - var b2: Int = 0 - var b3: [Int] = [] + var name: String = "" + var age: Int = 0 } } +