Skip to content

Commit

Permalink
更新readme
Browse files Browse the repository at this point in the history
  • Loading branch information
intsig171 committed Jul 26, 2024
1 parent 988127a commit 993e06b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
29 changes: 18 additions & 11 deletions Document/README/Usages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

它可以适应任何数据结构,包括嵌套的数组结构。
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
37 changes: 9 additions & 28 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 993e06b

Please sign in to comment.