ios - 从 '(_) throws -> ()'类型的 throw 函数无效转换为 '(Response) -> Void'类型的非 throw 函数

标签 ios swift error-handling

我需要处理这样的api错误代码,并为某些状态代码抛出错误。但是遵循代码显示了以上错误。我怎样才能做到这一点?

func login(data: [String: Any], completion: @escaping (ResponseModel<SignUpModel>?) -> Void) throws {
        NetworkAdapter.request(target: .login(data: data), success: { (response) in

            if let responseModel =  try? JSONDecoder().decode(ResponseModel<SignUpModel>.self,from: response.data) {
                switch responseModel.statusCode {
                case 2000:
                    completion(responseModel)
                case 4005:
                    throw ValidationError.athenticationFailure
                case .none,.some:
                    break
                }
                completion(responseModel)
            } else {
            }
        })
    }

最佳答案

你不能

throw ValidationError.athenticationFailure

因为请求是异步的。您可以将完成类型更改为Result<ResponseModel<SignUpModel>, ValidationError>以返回
completion(.success(responseModel))

成功与
completion(.failure(athenticationFailure)

失败。顺便说一句,我买了u😉

关于ios - 从 '(_) throws -> ()'类型的 throw 函数无效转换为 '(Response) -> Void'类型的非 throw 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61350402/

相关文章:

json - React,AWS GraphQL错误处理-JSON响应

javascript - 获取错误(已在 if 语句中定义变量)

iOS 应用程序永远不会退出 map

Swift CLLocationManager(didUpdateLocations) 调用过于频繁

ios - 无法在 Xcode 中编译 Storyboard并开始分配所有 RAM

ios - 导航栏搜索结果

ios - 删除 NSUserDefaults 到对应的 UITableViewCell

php - 启用 xdebug 时 Firebug 中无法读取的 var_dump

ios - 使用 UITapGestureRecognizer 更改标签颜色

android - Flutter - 'enableInteractiveSelection' 不适用于 EditableText