Skip to content

Instantly share code, notes, and snippets.

@avdwerff
Created May 28, 2016 09:12
Show Gist options
  • Save avdwerff/e293235174b0f904b342b3d047b3eab5 to your computer and use it in GitHub Desktop.
Save avdwerff/e293235174b0f904b342b3d047b3eab5 to your computer and use it in GitHub Desktop.
protocol JSONInitable {
init(data: JSON)
}
extension JSON {
func to<T where T: JSONInitable>(type: T.Type) -> [T] {
return self.arrayValue.map {
return T(data: $0)
}
}
}
//example
struct A:JSONInitable {
let a: String
let b: String
init(data: JSON) {
a = data["a"].stringValue
b = data["b"].stringValue
}
}
let data = JSON(["a": "A", "b": "B"])
let result = data.to(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment