I’m trying to return data from a closure so that I can use it outside the closure, but I can’t seem to work it out. I can update the Label in question from within the closure, but I would like to access the array outside the closure.
I’m using a UIView to display the data and the function below is called in viewWillAppear().
Would be grateful for help on this one,
Claire
Here’s the code:
func getInfo() {
let url = urlWithQuestion(postID)
let session = NSURLSession.sharedSession()
dataTask = session.dataTaskWithURL(url, completionHandler: {
data, response, error in
if let error = error where error.code == -999 {
return // Search was cancelled
} else if let httpResponse = response as? NSHTTPURLResponse where httpResponse.statusCode == 200 {
if let data = data, dictionary = self.parseJSON(data) {
self.searchResults = self.parseDictionary(dictionary)
dispatch_async(dispatch_get_main_queue()) {
self.questionLabel.text = self.searchResults[0].question
}
}
} else {
print("Failure! \(response!)")
}
dispatch_async(dispatch_get_main_queue()) {
self.showNetworkError()
}
})
dataTask?.resume()
}