Arb*_*tur 19 error-handling try-catch ios swift swift2
我在Swift 1.2中非常使用这种方法:NSURLConnection.sendSynchronousRequest(:_:_:_)
但在iOS9中显然已弃用.它仍然有效,但现在它使用新的Swift 2.0错误处理,我不知道如果它失败,我将如何得到错误消息,例如.如果时间用完了
我知道我必须把它放入do-catch然后说在try之前尝试但是我不知道如何捕获错误消息.
do {
let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)
return data
}
catch _ {
return nil
}
Run Code Online (Sandbox Code Playgroud)
之前我使用NSError然后使用它的描述属性,但现在我没有任何线索.
rsh*_*hev 36
使用自动error
变量,NSError
如果您愿意,可以将其强制转换为:
catch {
let nsError = error as NSError
print(nsError.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14153 次 |
最近记录: |