Created
December 4, 2016 09:44
-
-
Save MoathOthman/d7adae3bc8d79699556ec74936175d43 to your computer and use it in GitHub Desktop.
Objectmapper Transform Dictionary to Array of dictionary of the same type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct DictionaryToArrayTransform<T: Mappable>: TransformType { | |
init() {} | |
func transformFromJSON(_ value: Any?) -> [T]? { | |
guard let object = value as? [String: Any] else { | |
return nil | |
} | |
if let mapped = T(JSON: object) { | |
return [mapped] | |
} else { | |
return [] | |
} | |
} | |
func transformToJSON(_ value: [T]?) -> T? { | |
return value?.last | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment