Chapter 2 - Type 'Result<String, Error>' has no member 'error'

I got the error when using single, like the code below.

guard let path = Bundle.main.path(forResource: name, ofType: “txt”) else {
single(.error(FileReadError.fileNotFound))
return disposable
}

The error message from compiler is → Type ‘Result<String, Error>’ has no member ‘error’

Can somebody tell me how to solve this problem?

@lmw41 The Single<String> trait works with the Result<String, Error> enumeration under the hood, so you should use the Result<String, Error>.failure(Error) enumeration case instead of the SingleEvent<String>.error(Error) one to return errors from Single<String> traits in this case like this:

guard let path = Bundle.main.path(forResource: name, ofType: “txt”) else {
  single(.failure(FileReadError.fileNotFound))
  return disposable
}

You should also use the Result<String, Error>.failure(Error) enumeration case instead of the SingleEvent<String>.error(Error) one to throw errors from Single<String> traits in your code as follows:

loadText(from: "Copyright")
  .subscribe {
     switch $0 {
       case .success(let string):
         print(string)
       case .failure(let error):
         print(error)
     }
   }
  .disposed(by: disposeBag)

We will definitely fix this in the next edition of the book. Thank you!

2 Likes

Hello. This error is still in the book. Thanks for the reply. You helped me.

This topic was automatically closed after 166 days. New replies are no longer allowed.