我尝试使用" Yoda Speak" Mashape在Swift中的API。 我将打印API中的数据。 我成功地打电话给#34;回复" " getData"之后的方法方法,但我的模拟器在"响应"方法。 我知道那里有一些类似的问题,但我无法做到。 请给我一些建议。
func response(res: NSURLResponse!, data: NSData!, error: NSError!) {
println("response")
if error != nil {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
} else {
println("succeeded")
}
// simulator is out here.
var json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
for value in json {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
println(value)
})
}
}
func getData() {
println("getData")
// URL.
let url = NSURL(string: "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.++Oh+wait.")!
// URLRequest.
var req = NSMutableURLRequest(URL: url)
// header.
req.setValue("jY0bEhHCBpmsh8j1mpA5p11tCJGyp1tok3Zjsn4ubbvNNp5Jt3", forHTTPHeaderField: "X-Mashape-Key")
let connection: NSURLConnection = NSURLConnection(request: req, delegate: self, startImmediately: false)!
// Connection to the server.
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue(), completionHandler: self.response)
println("complete")
}