-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelMapper.swift
More file actions
60 lines (50 loc) · 1.71 KB
/
ModelMapper.swift
File metadata and controls
60 lines (50 loc) · 1.71 KB
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
//
// ModelMapper.swift
//
// Copyright © 2020 Arun Kappattiparambil Parameswaran. All rights reserved.
//
import Foundation
/// Converts model to another model with same properties
/// - Parameter modelItem: input Model object
func mapModelItem<T: Codable, U: Codable>(modelItem: T) -> U? {
if let dataEncoded = encode(objectModel: modelItem) {
if let convertedModelItem: U = decode(jsonData: dataEncoded) {
return convertedModelItem
}
}
return nil
}
/// Convert an array of models to another set of models
/// - Parameter modelItems: model items array
func mapModelItems<T: Codable, U: Codable>(modelItems: [T]) -> [U] {
var convertedModelItems: [U] = []
modelItems.forEach { (modelItem) in
if let convertedModelItem: U = mapModelItem(modelItem: modelItem) {
convertedModelItems.append(convertedModelItem)
}
}
return convertedModelItems
}
/// Parses JSON data to native swift object.
/// - Parameter jsonData: Data to be converted to swift object.
/// - returns: JSON parsed swift object. Returns nil if unable to parse the item.
fileprivate func decode<T: Codable>(jsonData : Data) -> T?{
do {
return try JSONDecoder().decode(T.self, from: jsonData)
} catch {
print(error.localizedDescription)
}
return nil
}
/// Map swift object to JSON data.
/// - Parameter objectModel: The model object from which encodes.
/// - returns: Data encoded from swift object. Returns nil if unable to encode the item.
fileprivate func encode<T: Codable>(objectModel: T) -> Data? {
do {
return try JSONEncoder().encode(objectModel)
}
catch {
print(error.localizedDescription)
}
return nil
}