Slide 15
Slide 15 text
func getBirds(completionHandler: [(Bird, UIImage)] -> Void) {
session.dataTaskWithURL(NSURL(string: "http://developer.apple.com/naming.json")!) {
(data, response, error) -> Void in
if let data = data, json = self.birdsJsonFromResponseData(data) {
var result: [(Bird, UIImage)] = []
for (index, bird) in json.enumerate() {
if let bird = self.parseBird(bird) {
self.getImage(bird) { image in
result.insert((bird, image), atIndex: min(result.count, index))
if result.count == json.count {
dispatch_async(dispatch_get_main_queue()) {
completionHandler(result)
}
}
}
}
}
}
}.resume()
}