Swift 2 - NSURLSession 不工作 [英] Swift 2 - NSURLSession Not working

查看:32
本文介绍了Swift 2 - NSURLSession 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样进行 NSURLSession 调用:

I am trying to do NSURLSession call like so:

let requestString = NSString(format: "http://api.com/api/Action/?tag=%d", Int(barcode)!) as String
let url: NSURL! = NSURL(string: requestString)

urlSession.dataTaskWithURL(url) {(data, response, error) -> Void in

    do {        
        let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! [AnyObject]    
        print(jsonData)

    } catch {


    }
}

我的问题是 do 语句中的代码没有被执行,所以打印语句没有被执行.我尝试将 print("Error") 放在 catch 语句中,但它甚至没有被执行.我正在尝试进行同步 NSURLSession 调用,我在这里缺少什么?

My issue is that the code inside the do statement does not get executed, so the print statement does not get executed. I tried putting a print("Error") inside the catch statement and that does not even get executed. I am trying to do a synchronous NSURLSession call, what am I missing here?

推荐答案

您需要在返回的 dataTask 上显式调用 resume 以使其执行

You need to call resume explicitly on returned dataTask to get it executed

let dataTask = session.dataTaskWithURL(...)
dataTask.resume

这篇关于Swift 2 - NSURLSession 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆