尝试访问Alamofire中的错误代码

编程入门 行业动态 更新时间:2024-10-21 18:47:35
本文介绍了尝试访问Alamofire中的错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Alamofire4.

I am using an Alamofire 4. When I do

print(response.debugDescription)

我在控制台中有类似的内容:

I have something like this in the console:

[Request]: api2.website [Response]: nil [Data]: 0 bytes [Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x17444ace0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x170490e50 [0x1ab165bb8]>{length = 16, capacity = 16, bytes = 0x100201bb341d1f890000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=api2.flowwow/api2/client/info/?auth_token=da88d8aa49ff6f8bb4e1&hash=7f38be3f68db39a6d88687505fdb9ba5&partner_id=1004, NSErrorFailingURLKey=api2.website, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The Internet connection appears to be offline.} [Timeline]: Timeline: { "Request Start Time": 510763454.078, "Initial Response Time": 510763455.293, "Request Completed Time": 510763455.293, "Serialization Completed Time": 510763455.297, "Latency": 1.215 secs, "Request Duration": 1.215 secs, "Serialization Duration": 0.005 secs, "Total Duration": 1.220 secs }

我感兴趣的是以下一行:

And there is a particular line which interests me:

Error Domain=NSURLErrorDomain Code=-1009

如何获取此代码,以便我可以正确处理该错误.我尝试了所有可能组成的组合,但是任何地方都找不到此代码.

How can I get this Code so I can handle the error correctly. I tried all combinations I could make up but there is no trace of this code anywhere.

推荐答案

当您使用Alamofire进行呼叫时,它将返回一个响应,您可以在其中检查是否有任何错误.这是使用Alamofire进行错误处理的简单示例.

when you make calls with Alamofire, it returns a response where you can check for any errors. This is a simple example of error handling call with Alamofire.

Alamofire.request("your.url").responseJSON { response in if (response.result.isSuccess){ //do your json stuff } else if (response.result.isFailure) { //Manager your error switch (response.error!._code){ case NSURLErrorTimedOut: //Manager your time out error break case NSURLErrorNotConnectedToInternet: //Manager your not connected to internet error break default: //manager your default case } } }

享受:)

于2020年4月1日更新

此代码应在Alamofire 5版本上有效.我仍然没有检查,让我知道这是否有效

This code should works on Alamofire 5 version. I still didn't check, let me know if this works

AF.request(route).responseJSON { (response) in let result = response.result switch result { case .success(let value): print("Success") // Do something with value case .failure(let error): if let underlyingError = error.underlyingError { if let urlError = underlyingError as? URLError { switch urlError.code { case .timedOut: print("Timed out error") case .notConnectedToInternet: print("Not connected") default: //Do something print("Unmanaged error") } } } } }

我希望这能奏效:)

更多推荐

尝试访问Alamofire中的错误代码

本文发布于:2023-11-26 18:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误代码   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!