在alamofire 2中,他们介绍了结果类型:
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.responseJSON { request, response, result in
switch result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
case .Failure(let data, let error):
print(error)
}
}
error 的类型为 ErrorType ,其唯一的成员是 debugDescription ,它打印:
可选(错误域= NSURLErrorDomain代码= -1009“Es besteht anscheinend keine Verbindung zum Internet . ”UserInfo = {NSUnderlyingError = 0x135f4e7c0 {Error Domain = kCFErrorDomainCFNetwork Code = -1009“(null)”UserInfo = {_ kCFStreamErrorCodeKey = 8,_kCFStreamErrorDomainKey = 12}},NSErrorFailingURLStringKey = myurl,NSErrorFailingURLKey = myurl,_kCFStreamErrorDomainKey = 12,_kCFStreamErrorCodeKey = 8,NSLocalizedDescription = Es besteht anscheinend keine Verbindung zum Internet . })
如何从错误而不是整个调试消息中获取NSLocalizedDescription?
在alamofire 1中,错误类型为NSError,可能会收到错误消息:
error.localizedDescription
然而,这似乎在alamofire 2中是不可能的 . 任何想法?
3 回答
在"Alamofire 2.0 Migration Guide"中声明了一种新的结果类型
介绍了,Alamofire仍然只生成NSError对象 .
在"Why and how any ErrorType can always be casted to NSError?"中,Apple Stuff的成员确认
ErrorType
始终可以转换为NSError
:因此,这会在快速测试中编译并打印预期结果(例如“无法连接到服务器” . ):
ErrorType可以转换为NSError . 试试这个:
它是错误对象上的String: