0
我试图从用Swift编写的iOS应用程序连接到数据库。我无法找到替换所有其他的功能,这些功能在最新的iOS更新(9.2?)中被标记为已弃用。其中一些功能是sendSynchronousRequest和sendAsynchronousRequest。无法读取Swift AsynchronousRequest响应
我想从数据库中读取数据和响应。我使用以下代码:
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)抛出 - >虚空”非抛功能型‘(?NSURLResponse?NSData的?NSError) - >虚空’
然而,当我注释掉if语句行(让jsonData:NSDictionary的=尝试...)错误消失。
我知道这个函数已被弃用;我找不到其他东西。
如何从AsynchronousRequest中读取没有错误的响应?