获取 Alamofire AFError 错误 Swift 的字符串值

Posted

技术标签:

【中文标题】获取 Alamofire AFError 错误 Swift 的字符串值【英文标题】:Get string value of Alamofire AFError error Swift 【发布时间】:2021-06-06 18:57:30 【问题描述】:

我正在使用 AlamoFire 向我的服务器发出请求,并且我有一个类似这样的设置,其中“self.error”是一个字符串变量。

                    session.request("https://localhost:3000/my-route", method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON  response in
                        switch response.result 
                        case .success(let result):
                              print("nice", result)                         
                            
                        case .failure(let err):
                            self.error = err //self.error is a String state variable
                            print("failure")
                        
                       

但是,我收到(合理的)错误“无法将 'AFError' 类型的值分配给 'String' 类型”,因为我正在尝试设置 self.error = err,而 err 是 AFError 类型,而 self .error 是一个字符串状态变量。

我意识到我可以做一些类似 self.error = "There was an error" 的事情,但我想向用户提供一些关于错误实际是什么的反馈,但我似乎找不到正确的属性(例如 err.stringValue、err.description 等)来执行此操作。似乎这里有一些源代码https://github.com/Alamofire/Alamofire/blob/master/Source/AFError.swift,但我不确定这与我想获得的字符串值有什么关系。

我们将不胜感激。

【问题讨论】:

【参考方案1】:

你可以试试err.localizedDescription或者你可以在函数完成中得到错误并返回completion(error)

【讨论】:

非常感谢!以为 .localizedDescription 仅适用于 Firebase,但猜不是!【参考方案2】:

对于这种情况,您始终可以使用字符串插值,例如self.error = "\(err)",但这不是一个好主意,因为 err 不只是一个字符串,它可能包含以下内容:

sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x280e37750 Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo=_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <78576E38-D021-4ADC-87D4-1C9D81FF0E3A>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <78576E38-D021-4ADC-87D4-1C9D81FF0E3A>.<1>" ), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=https://localhost:3000/my-route, NSErrorFailingURLKey=https://localhost:3000/my-route, _kCFStreamErrorDomainKey=1)

就像@Menaim 已经提到的,你可以使用err.localizedDescription,然后你可以得到类似URLSessionTask failed with error: Could not connect to the server.的东西

一般来说,我不会将错误直接传递给用户。这取决于您的用户,但他们真的应该知道什么吗? URLSessionTask是?

在我看来,你应该为用户定义错误字符串,让每个用户都能理解,你应该将err.localizedDescription 记录到控制台,这样你就可以知道发生了什么。

如果您的应用支持多种语言,您可以定义 localizable.strings 文件并翻译错误。

如果你这样做print("failure"),每次出现错误,如何区分错误?

我的建议是这样的:

                case .failure(let err):
                    self.error = "There was an error connecting to the server"
                    print("Failed to connect to backend: \(err.localizedDescription)")

【讨论】:

是的,谢谢!这就是我最终要做的——我给了用户一个更一般的错误,并将一些东西打印到控制台以用于开发/调试目的!

以上是关于获取 Alamofire AFError 错误 Swift 的字符串值的主要内容,如果未能解决你的问题,请参考以下文章

获取 Alamofire AFError 错误 Swift 的字符串值

使用消息“Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType”从存储中获取图像时出错

Alamofire.AFError.responseValidationFailed

解析 JSON 时出现 AFError Alamofire 5

从 'AFError' 到 'AFError' 的条件转换总是成功

使用 Flickr API 时出现 Alamofire AFError