Skip to content

Commit 3bb3c9e

Browse files
committed
mapObject and mapArray for Argo and Himotoki
1 parent 3cbc41b commit 3bb3c9e

File tree

8 files changed

+95
-58
lines changed

8 files changed

+95
-58
lines changed

Example/Argo Sample/Model/RepoRequest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ extension RepoRequest {
4343
func response(from object: Any, urlResponse: HTTPURLResponse) -> [Repo] {
4444
guard
4545
let tree = object as? [String: Any],
46-
let repos = decode(tree, rootKey: "items") as [Repo]?
46+
let items = tree["items"],
47+
let repos = try? Repo.mapArray(items)
4748
else {
4849
return []
4950
}

Example/Himotoki Sample/Assets.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -30,6 +40,16 @@
3040
"size" : "60x60",
3141
"scale" : "3x"
3242
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "20x20",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "20x20",
51+
"scale" : "2x"
52+
},
3353
{
3454
"idiom" : "ipad",
3555
"size" : "29x29",
@@ -59,6 +79,11 @@
5979
"idiom" : "ipad",
6080
"size" : "76x76",
6181
"scale" : "2x"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"size" : "83.5x83.5",
86+
"scale" : "2x"
6287
}
6388
],
6489
"info" : {

Example/Himotoki Sample/Model/RepoRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension RepoRequest {
4545
guard
4646
let tree = object as? [String: Any],
4747
let items = tree["items"],
48-
let repos: [Repo] = try? decodeArray(items)
48+
let repos = try? Repo.mapArray(items)
4949
else {
5050
return []
5151
}

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 29 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Classes/Argo/APIKit+Argo.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@
77
//
88

99
import Foundation
10+
import Argo
11+
12+
public extension Decodable where Self == Self.DecodedType {
13+
public static func mapObject(_ object: Any) throws -> Self {
14+
guard let result = Self.decode(JSON(object)).value else {
15+
throw MapError.Unmatchable(object)
16+
}
17+
return result
18+
}
19+
public static func mapArray(_ object: Any) throws -> [Self] {
20+
guard let result = Array<Self>.decode(JSON(object)).value else {
21+
throw MapError.Unmatchable(object)
22+
}
23+
return result
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Extension.swift
3+
// Pods
4+
//
5+
// Created by DTVD on 1/9/17.
6+
//
7+
//
8+
9+
enum MapError: Error {
10+
case Unmatchable(Any)
11+
}

Sources/Classes/Himotoki/APIKit+Himotoki.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@
77
//
88

99
import Foundation
10+
import Himotoki
11+
12+
public extension Decodable {
13+
public static func mapObject(_ object: Any) throws -> Self {
14+
return try Self.decodeValue(object)
15+
}
16+
17+
public static func mapArray(_ object: Any) throws -> [Self] {
18+
return try [Self].decode(object)
19+
}
20+
}

Sources/Classes/ObjectMapper/APIKit+ObjectMapper.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import Foundation
1010
import ObjectMapper
1111

12-
enum MapError: Error {
13-
case Unmatchable(Any)
14-
}
15-
1612
public extension Mappable {
1713

1814
public static func mapObject(_ object: Any) throws -> Self {

0 commit comments

Comments
 (0)