如何在 RxSwift 中使用 EventConvertible 创建客户观察者事件?
How to create customer Observer Event using EventConvertible in RxSwift?
例如,在下面的代码中,默认的 ObserverType 有一个事件 onError,其中可以传递 Swift.Error 对象。我如何创建自定义 ObserverType / Event 以便我能够提供自定义 class.
的对象
class LoginService: LoginServiceProtocol {
func signIn(with credentials: Credentials) -> Observable<User> {
return Observable.create { observer in
/*
Networking logic here.
*/
observer.onNext(User()) // Simulation of successful user authentication.
observer.onError(<#T##error: Error##Error#>) // want to user custom class object instead of Swift.Error object here
return Disposables.create()
}
}
}
唯一的方法是让自定义 class 对象符合 Swift.Error
协议。
class MyCustomClassObject: Error {
// your custom stuff here
}
例如,在下面的代码中,默认的 ObserverType 有一个事件 onError,其中可以传递 Swift.Error 对象。我如何创建自定义 ObserverType / Event 以便我能够提供自定义 class.
的对象class LoginService: LoginServiceProtocol {
func signIn(with credentials: Credentials) -> Observable<User> {
return Observable.create { observer in
/*
Networking logic here.
*/
observer.onNext(User()) // Simulation of successful user authentication.
observer.onError(<#T##error: Error##Error#>) // want to user custom class object instead of Swift.Error object here
return Disposables.create()
}
}
}
唯一的方法是让自定义 class 对象符合 Swift.Error
协议。
class MyCustomClassObject: Error {
// your custom stuff here
}