For example, I have the following code:
let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
var dann = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.str = dann
})
task.resume()
I want to transfer the data to a variable in the class (the str variable in the class). The string "self.str = dann" does not convey anything. How can I do this?
Can you try dispatch_async?
// Swift 3 based for Quick ref.
I'm not sure NSString is the type you want. JSON may be format of the data returned, depending on your URL's functionality. I tried the code provided and got the same issues, but if you treat it as JSON (I used httpbin.org as a dummy URL source) and it worked.
(thanks for suggested edit @sgthad. the edit is not particularly relevant to the question, but still wanted to update the code to be current.)
Update for Swift 3 syntax