The method fails to return a value got from a GET request where variable res
can be printed in the task section, but fails to return it in the end.
func lookUpTheWord(word:String) -> NSDictionary {
var res = NSDictionary()
let urlString = "https://wordsapiv1.p.mashape.com/words/" + word
if let url = NSURL(string:urlString) {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("this is a key", forHTTPHeaderField: "X-Mashape-Key")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("\(error)")
return
}
res = self.toJson(data!)
print(res) //it works here
}
task.resume()
}
return res //res becomes nil
}