简体   繁体   English

实例方法“flatMap”要求“String”符合“ObservableConvertibleType”

[英]Instance method 'flatMap' requires that 'String' conform to 'ObservableConvertibleType'

I am getting below build error for RxSwift ,我遇到了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'实例方法“flatMap”要求“String”符合“ObservableConvertibleType”

What am I missing?我错过了什么?

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.在第一行中,您告诉编译器闭包返回一个StringflatMap要求闭包返回一个 Observable 类型。

Also, FYI: a.flatMap { .just($0) } is effectively a no-op.另外,仅供参考: a.flatMap { .just($0) }实际上是无操作的。 In other words: aa.flatMap { .just($0) }换句话说: aa.flatMap { .just($0) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:实例方法 'onReceive(_:perform:)' 要求 'Int' 符合 'Publisher' - Error: Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher' VStack 显示错误“实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'” - VStack shows error "Instance method 'sheet(item:onDismiss:content:)' requires that 'Int' conform to 'Identifiable'" 为什么我收到错误“实例方法 'fill(_:style:)' 要求 'some View' 符合 'ShapeStyle'”? - Why am I getting the error "Instance method 'fill(_:style:)' requires that 'some View' conform to 'ShapeStyle'"? ForEach 要求 '[String: String]' 符合 'Identifiable' - ForEach requires that '[String : String]' conform to 'Identifiable' class 方法要求 'AFDataResponse<x> ' 符合 Encodable &amp; Encodable</x> - class method requires that 'AFDataResponse<X>' conform to Encodable & Encodable MCSessionDelegate要求类符合NSObjectProtocol - MCSessionDelegate Requires Class to Conform to NSObjectProtocol 数组要求 'Element' 符合 'Equatable' - Array requires that 'Element' conform to 'Equatable' 在单独的类中使用func()需要拥有该方法的类的实例 - Using func() in seperate class requires instance of the class that owns the method Swift:初始化程序 &#39;init(_:)&#39; 要求 &#39;Decimal&#39; 符合 &#39;BinaryInteger&#39; - Swift: initializer 'init(_:)' requires that 'Decimal' conform to 'BinaryInteger' 运算符 function '==' 要求 'Entry' 符合 'Equatable' - Operator function '==' requires that 'Entry' conform to 'Equatable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM