【发布时间】:2016-01-22 12:53:52
【问题描述】:
我正在尝试从用 Swift 编写的 iOS 应用程序连接到数据库。我找不到替代所有其他功能的功能,这些功能在最新的 iOS 更新(9.2?)中被标记为已弃用。其中一些函数是 sendSynchronousRequest 和 sendAsynchronousRequest。
我想从数据库中读取数据和响应。我正在使用以下代码:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
let res = response as! NSHTTPURLResponse
if(res.statusCode >= 200 && res.statusCode < 300) {
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
})
我在第一行遇到错误:
从“(NSURLResponse?, NSData?, NSError?) throws -> Void' 类型的抛出函数到非抛出函数类型“(NSURLResponse?, NSData?, NSError?) -> Void”的无效转换
但是,当我注释掉 if 语句中的行(让 jsonData:NSDictionary = try ...)时,错误消失了。
我知道这个功能已被弃用;我找不到其他任何东西。
如何才能正确读取 AsynchronousRequest 的响应?
【问题讨论】: