Skip to content

Commit 4ad0225

Browse files
committed
Refactor Mapper with filterMap function
1 parent a65ed3f commit 4ad0225

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

ObjectMapper/Core/Mapper.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,8 @@ public final class Mapper<N: Mappable> {
228228
* Maps an array of JSON dictionary to an array of object that conforms to Mappable
229229
*/
230230
public func mapArray(JSONArray: [[String : AnyObject]]) -> [N] {
231-
return JSONArray.reduce([]) { (var values, JSON) in
232-
// map every element in JSON array to type N
233-
if let value = self.map(JSON) {
234-
values.append(value)
235-
}
236-
return values
237-
}
231+
// map every element in JSON array to type N
232+
return JSONArray.filterMap(map)
238233
}
239234

240235
/** Maps a JSON object to a dictionary of Mappable objects if it is a JSON
@@ -252,15 +247,8 @@ public final class Mapper<N: Mappable> {
252247
* Maps a JSON dictionary of dictionaries to a dictionary of objects that conform to Mappable.
253248
*/
254249
public func mapDictionary(JSONDictionary: [String : [String : AnyObject]]) -> [String : N] {
255-
return reduce(JSONDictionary, [String: N]()) { (var values, element) in
256-
let (key, value) = element
257-
258-
// map every value in dictionary to type N
259-
if let newValue = self.map(value) {
260-
values[key] = newValue
261-
}
262-
return values
263-
}
250+
// map every value in dictionary to type N
251+
return JSONDictionary.filterMap(map)
264252
}
265253

266254
// MARK: Functions that create JSON from objects

0 commit comments

Comments
 (0)