我正在尝试快速创建一个通用的结果枚举,这就是我到目前为止所拥有的:
enum Result<T: Codable>: Error {
//Indicates that it was a success, and data is the returned result
case Success(data: T)
//Indicates that there was an unrecognizable error
case Failure(error: Error)
//Some cases for specific status codes
case Forbidden //Status code: 403
case NotAcceptable //Status code: 406
case Conflict //Status code: 409
case InternalServerError //Status code: 500
}
然后我尝试从中创建一个 Observable ,如下所示:(T 在函数调用中指定,为简洁起见缩短)
Observable<Result<T>>.create { observer in
//Some code to go make an Http requests and return response....
switch response.result {
case .success(let value):
//This works fine
observer.onNext(Result.success(value))
observer.onCompleted()
case .failure(let error):
//This is where things go wrong.
observer.onError(Result.failure(error))
}
}
问题是当我尝试在.failure
案例中返回时,它总是说Argument type 'Result<_>' does not conform to expected type 'Error'
即使 Result 是错误类型
难道我做错了什么?