Skip to content

Commit

Permalink
smartAny支持Model
Browse files Browse the repository at this point in the history
  • Loading branch information
intsig171 committed Aug 7, 2024
1 parent 5c85d7f commit 83c85f0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PODS:
- FBSnapshotTestCase/SwiftSupport (2.1.4):
- FBSnapshotTestCase/Core
- HandyJSON (5.0.0-beta.1)
- SmartCodable (4.1.5)
- SmartCodable (4.1.6)
- SnapKit (5.6.0)

DEPENDENCIES:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: bff8d6fabca0b5973d14a0faf6f66fa7e7c8311d
SmartCodable: 3dfe748436ce6ccf6a8f04e48171d8d7a3d7b879
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25

PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SmartCodable.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 19 additions & 39 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,33 @@ class Test2ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

let subModel = SubModel()
let model = Model(type: 2, data: subModel)

let dict = [
"nick_name": "Mccc",
"self_age": 10,
"dict": ["k":"l"]
] as [String : Any]

guard let smartModel = SmartModel.deserialize(from: dict) else { return }
BTPrint.print(smartModel.dict)
smartPrint(value: smartModel)
let json = model.toJSONString()

print(json)
}
}

struct SmartModel: SmartCodable {

var nick_name: String?
@IgnoredKey
var dict: NSMutableDictionary = .init()
var self_age: String?

static func mappingForValue() -> [SmartValueTransformer]? {
[
CodingKeys.dict <--- DictTransformer()
]
}
}

struct DictTransformer: ValueTransformable {
func transformFromJSON(_ value: Any) -> NSMutableDictionary? {

if let dict = value as? NSDictionary {
let dictM = NSMutableDictionary(dictionary: dict)

print("dictM = \(dictM)")
return dictM
class Model: SmartCodable {
init(type: Int, data: Any) {
self.type = type
self.data = data
}

return nil
var type: Int?

@SmartAny
var data: Any?

// (value as? NSDictionary)?.mutableCopy() as? NSMutableDictionary
required init() { }
}

func transformToJSON(_ value: NSMutableDictionary) -> [String : Any]? {
value as? [String: Any]


class SubModel: SmartCodable {
var name: String = "mccc"
required init() { }
}

typealias Object = NSMutableDictionary
typealias JSON = [String: Any]
}
2 changes: 1 addition & 1 deletion SmartCodable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

Pod::Spec.new do |s|
s.name = 'SmartCodable'
s.version = '4.1.5'
s.version = '4.1.6'
s.summary = '数据解析库'

s.homepage = 'https://github.com/intsig171'
Expand Down
2 changes: 2 additions & 0 deletions SmartCodable/Classes/SmartType/SmartAny/SmartAny.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public struct SmartAny<T>: Codable {
} else if let arr = wrappedValue as? [Any] {
let value = arr.cover
try container.encode(value)
} else if let model = wrappedValue as? SmartCodable {
try container.encode(model)
} else {
let value = SmartAnyImpl(from: wrappedValue)
try container.encode(value)
Expand Down

0 comments on commit 83c85f0

Please sign in to comment.