Skip to content

Instantly share code, notes, and snippets.

@MoathOthman
Created December 4, 2016 09:44
Show Gist options
  • Save MoathOthman/d7adae3bc8d79699556ec74936175d43 to your computer and use it in GitHub Desktop.
Save MoathOthman/d7adae3bc8d79699556ec74936175d43 to your computer and use it in GitHub Desktop.
Objectmapper Transform Dictionary to Array of dictionary of the same type
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