ios - 从 '(_) throws -> ()' 类型的抛出函数到非抛出函数类型 '(Response) -> Void' 的无效转换
问题描述
我需要像这样处理 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
推荐阅读
- python - 如何从包含其名称的字符串中选择一个变量?
- android - 如何通过自定义对象从 Firestore 获取文档内容?(安卓 - 科特林)
- vue.js - 是否有在 vue-dropzone 消息区域中加载自定义 HTML 元素的有效解决方案?
- python - 如何处理发生在 Keras 的自定义层中的代码错误?
- javascript - 在 this.state 中使用 JSX,但它被呈现为纯文本
- flutter - Flutter 应用会产生过多的缓存吗?
- html - 导航菜单显示在桌面,但不显示在移动视图中
- python - 了解 GIL 对并发服务器的性能影响
- swift - 如何验证使用 XCTAssert 调用了类方法?
- google-bigquery - `WriteToBigQuery` 和 `BigQuerySink` 之间有什么区别