Search code examples
rx-swiftrx-cocoa

How to create customer Observer Event using EventConvertible in RxSwift?


For example in the following code the default ObserverType has an event onError where Swift.Error object can be passed. How can I create custom ObserverType / Event so that I'll be able to supply an object of a custom 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()
    }
}
}

Solution

  • The only way to do it is to have the custom class object conform to the Swift.Error protocol.

    class MyCustomClassObject: Error {
        // your custom stuff here
    }