So I am trying to get a simple string of data back from a RESTful API. Here is what a response looks like:
Hello
It is not formatted in XML or JSON or anything just a simple string because only one word is being passed back at a time. So here is what my swift looks like:
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
println("error: \(error.localizedDescription): \(error.userInfo)")
}
var withNewLine:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!
let str:NSString = withNewLine.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
dispatch_async(dispatch_get_main_queue(), {
self.spellCorrection = str
})
})
task.resume()
I get the correct data back in the "str" variable before I go into the dispatch_async() method. But when I do go into the dispatch_async() method the "str" variable becomes nil and I am not sure why. I am just trying to save the single word in a variable within my class, so if I am going about this completely wrong let me know. I would really appreciate any help I can get. Thanks!
Edit
Okay so I should have included this the first time I posted. I have also tried this:
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
println("error: \(error.localizedDescription): \(error.userInfo)")
}
var withNewLine:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.spellCorrection = withNewLine.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
})
task.resume()
But when I do this the string does not stay in the self.spellCorrection variable after the completionHandler has finished. So my question is, what should I do to keep the string in the self.spellCorrection variable after the completionHandler has finished? Thanks!
More details
In viewDidLoad:
var spellCorrection: NSString = ""
//First print
println(self.spellCorrection)
In the method I call later I have:
func spellCheck() {
var check: String = "http://theURL.com/?text=" + condenseWhitespace(self.lastTypedWord)
let url = NSURL(string: check)
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
println("error: \(error.localizedDescription): \(error.userInfo)")
}
var withNewLine:NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.spellCorrection = withNewLine.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}).resume()
//Print after the completionHandler
println(self.spellCorrection)
}