Skip to content

Instantly share code, notes, and snippets.

@sgoodwin
Last active August 29, 2015 14:02
Show Gist options
  • Save sgoodwin/a1aca7e2ef379b53c957 to your computer and use it in GitHub Desktop.
Save sgoodwin/a1aca7e2ef379b53c957 to your computer and use it in GitHub Desktop.
Generic function to make an array of types from incoming JSON
protocol FromJSONAble {
init(JSONDictionary: NSDictionary)
}
extension NSJSONSerialization {
class func unmarshallArray<T: FromJSONAble>(data: NSData)-> T[] {
var jsonError: NSError?
let roughResults = self.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &jsonError) as NSArray
var results = T[]()
for object : AnyObject in roughResults {
results.append(T(JSONDictionary: object as NSDictionary))
}
return results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment