Created
May 28, 2016 09:12
-
-
Save avdwerff/e293235174b0f904b342b3d047b3eab5 to your computer and use it in GitHub Desktop.
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
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