相关疑难解决方法(0)

在Swift 2中处理异步闭包错误的最佳方法是什么?

我使用了很多异步网络请求的(顺便说一句任何网络请求的iOS需要通过异步),我发现的方式来更好地应对来自苹果的错误,dataTaskWithRequest它并不支持throws.

我有这样的代码:

func sendRequest(someData: MyCustomClass?, completion: (response: NSData?) -> ()) {
    let request = NSURLRequest(URL: NSURL(string: "http://google.com")!)

    if someData == nil {
        // throw my custom error
    }

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        // here I want to handle Apple's error
    }
    task.resume()
}
Run Code Online (Sandbox Code Playgroud)

我需要解析我可能的自定义错误并处理可能的连接错误dataTaskWithRequest.引入了Swift 2 throws,但你不能抛弃Apple的关闭,因为它们没有抛出支持并且运行异步.

我只看到添加到我的完成块NSError返回的方法,但据我所知使用的NSError是旧式的Objective-C方式.ErrorType只能用于投掷(afaik).

使用Apple网络闭包时,处理错误的最佳和最现代的方法是什么?根据我的理解,任何异步网络功能都没有办法投入使用吗?

asynchronous throw nserror swift

6
推荐指数
1
解决办法
3063
查看次数

标签 统计

asynchronous ×1

nserror ×1

swift ×1

throw ×1