2

I am getting below build error for RxSwift,

func testFlatMap() {

    let bag = DisposeBag()
    let subject = PublishSubject<String>.init()

    subject.flatMap({ (value) -> String in
            PublishSubject.just(value)
        }).subscribe(

        onNext: { value in
            print(value)
        }

    ).disposed(by: bag)

    subject.on(.next("Test"))
}

Instance method 'flatMap' requires that 'String' conform to 'ObservableConvertibleType'

What am I missing?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

1 Answers1

6

This is the problem:

subject.flatMap({ (value) -> String in
    PublishSubject.just(value)
})

In the first line you are telling the compiler that the closure returns a String but flatMap requires the closure to return an Observable type.

Also, FYI: a.flatMap { .just($0) } is effectively a no-op. In other words: aa.flatMap { .just($0) }

Daniel T.
  • 32,821
  • 6
  • 50
  • 72