forked from hulinSun/Moya-HandyJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse+HandyJSON.swift
67 lines (47 loc) · 1.73 KB
/
Response+HandyJSON.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Response+HandyJSON.swift
// MyRx
//
// Created by Hony on 2016/12/29.
// Copyright © 2016年 Hony. All rights reserved.
//
import Foundation
import Moya
import HandyJSON
public extension Response {
/// 整个 Data Model
public func mapObject<T: HandyJSON>(_ type: T.Type) -> T? {
guard let dataString = String.init(data: self.data, encoding: .utf8),
let object = JSONDeserializer<T>.deserializeFrom(json: dataString)
else {
return nil
}
return object
}
/// 制定的某个 Key 对应的模型
public func mapObject<T: HandyJSON>(_ type: T.Type ,designatedPath: String) -> T?{
guard let dataString = String(data: self.data, encoding: .utf8),
let object = JSONDeserializer<T>.deserializeFrom(json: dataString, designatedPath: designatedPath)
else {
return nil
}
return object
}
/// Data 对应的 [Model]
public func mapArray<T: HandyJSON>(_ type: T.Type) -> [T?]? {
guard let dataString = String(data: self.data, encoding: .utf8),
let object = JSONDeserializer<T>.deserializeModelArrayFrom(json: dataString)
else {
return nil
}
return object
}
/// Data 某个Key 下对应的 的 [Model]
public func mapArray<T: HandyJSON>(_ type: T.Type ,designatedPath: String ) -> [T?]? {
guard let dataString = String(data: self.data, encoding: .utf8),
let object = JSONDeserializer<T>.deserializeModelArrayFrom(json: dataString , designatedPath: designatedPath)
else {
return nil
}
return object
}
}