谁能解释这里的错误是什么?
Member 'success' in 'SingleEvent<_>' produces result of type 'SingleEvent<Element>', but context expects 'SingleEvent<_>'
如果结果案例是.success我想在单个事件中传递JSON,我返回一个单一的错误或成功的泛型类型
这是完整的代码
import Foundation
import RxSwift
public class APIClient<T: APIProtocol> {
var disposeBag: DisposeBag = DisposeBag()
}
extension APIClient {
public func get<U>(apiService: T, key: String) -> Single<U> {
return Single.create { single in
let task = TaskRequestManager.run(apiService, completion: { result in
DispatchQueue.main.async {
switch result {
case .error(let error):
single(.error(error))
case .success(let json):
guard let JSON = json[key] as? [String: Any] else {
single(.error(ApiError.jsonParsingFailure))
return
}
single(.success(JSON))
}
}
})
task.resume()
return Disposables.create {
task.cancel()
}
}
}
}
改变这一点
return Single.create { single in
进入这个
return Single<U>.create { single in
返回类型的get
函数是Single<U>
,而调用single(.success(JSON))
通过指定具体类型Single<[String: Any]>
违反了这一点。要么在U
函数中用Dictionary<String, Any>
替换泛型类型get
,要么使TaskRequestManager.run
接受泛型类型。