Ios Swift 2中Alamofire的错误处理
您好,我正在练习一些代码,我的代码可以使用Ios Swift 2中Alamofire的错误处理,ios,error-handling,swift2,alamofire,Ios,Error Handling,Swift2,Alamofire,您好,我正在练习一些代码,我的代码可以使用案例。成功(let值):并显示警报,但将案例。失败(let错误):使用4xx类状态代码显示警报 Alamofire.request(.GET, URL).responseJSON { (response) -> Void in if let value = response.result.value { let json = JSON(value) switch r
案例。成功(let值):
并显示警报,但将案例。失败(let错误):
使用4xx类状态代码显示警报
Alamofire.request(.GET, URL).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
switch response.result {
case .Success(let value):
let name = json["name"].string
if let nothing = name {
self.alertMessage(message: "Name not Found")
} else {
self.alertMessage(message: "Name Found")
}
case .Failure(let error):
self.alertMessage(message: "Error 4xx / 5xx")
}
您可以使用
validate
检查状态代码:
Alamofire.request(.GET, URL)
.validate() // or, if you just want to check status codes, validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .Success(let value):
let json = JSON(value)
if let name = json["name"].string {
self.alertMessage(message: "Name Found: \(name)")
} else {
self.alertMessage(message: "Name not Found")
}
case .Failure(let error):
self.alertMessage(message: "Error 4xx / 5xx: \(error)")
}
}
Alamofire.request(.GET,URL)
.validate()//或者,如果您只想检查状态代码,请验证(状态代码:200。。
Alamofire.request(url)
.validate() // or, if you just want to check status codes, validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
if let name = json["name"].string {
self.alertMessage(message: "Name Found: \(name)")
} else {
self.alertMessage(message: "Name not Found")
}
case .failure(let error):
self.alertMessage(message: "Error 4xx / 5xx: \(error)")
}
}