I need to use data from a JSON web service to update controls in a SWIFT app. I can get the data no problem, but I can't seem to access the UI controls from within the task block and I can't get the JSON to persist out of the block. I've searched around and haven't found the answer. Here's my test code. The current result is that value1Result has a value inside task, but is nil outside. Thanks in advance.
var jsonResult:NSDictionary!
var value1Result:String!
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { data, response, error in
var error: NSError?
jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)!
as! Dictionary<String, AnyObject>
println(jsonResult)
if let value1 = jsonResult["value1"] {
println(value1)
value1Result = value1 as! String
}
}
task.resume()
self.textView1.text = value1Result