-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseExtension.swift
37 lines (33 loc) · 1 KB
/
ResponseExtension.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
//
// NetworkExtensions.swift
// Owl Monitoring
//
// Created by 김동혁 on 2017. 10. 10..
// Copyright © 2017년 KongTech. All rights reserved.
//
import Foundation
import Moya
extension Response {
func mapObject<T>(_ type: T.Type) throws -> T where T : Decodable {
let decoder = JSONDecoder()
do {
return try decoder.decode(type, from: data)
} catch (let error) {
throw error
}
}
func mapObject<T>(_ type: T.Type, key: String) throws -> T where T : Decodable {
let decoder = JSONDecoder()
do {
let map = try mapJSON() as! [String : Any]
if let data = map[key] {
let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
return try decoder.decode(type, from: jsonData)
} else {
throw MoyaError.jsonMapping(self)
}
} catch (let error) {
throw error
}
}
}