Last active
August 29, 2015 14:02
-
-
Save sgoodwin/a1aca7e2ef379b53c957 to your computer and use it in GitHub Desktop.
Generic function to make an array of types from incoming JSON
This file contains hidden or 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 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