Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 988
Sazzad Hissain Khan Avatar asked Jan 18 '26 23:01

Sazzad Hissain Khan


1 Answers

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) }

like image 68
Daniel T. Avatar answered Jan 20 '26 12:01

Daniel T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!