带有 resultSelector 的 RxSwift withLatestFrom 无法编译

RxSwift withLatestFrom with resultSelector doesn't compile

我有一个 Driver 类型的 Bool 和一个 BehaviorRelay 类型的 Page (这是一个自定义枚举)。

enum Page {
    case option1(CustomClass1, CustomClass2)
    case option2(CustomClass3)
    case option3(CustomClass4)

    var property1: CustomClass2? {
        switch self {
        case .option1(_, let custom):
            return custom
        case .option2, .option3:
            return nil
        }
    }
}

我在另一个 ViewModel 中有 Driver<Bool>

class ViewModel1 {
    struct Output {
        let hasItems: Driver<Bool>
    }

    let output: Output

    init() {
        let hasItemsRelay: BehaviorRelay<Bool> = BehaviorRelay<Bool>(value: false)

        self.output = Output(
            hasItems: hasItemsRelay.asDriver()
        )
    }
}

我的基地里有一个 BehaviorRelay<Page?> class。

class ViewModel2 {
    let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)

    init() {
        self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
    }
}

ViewModel2 class 中,我试图在 ViewModel1.InputhasItems 驱动程序上捕获一个事件,当我得到一个事件时,我需要当前值currentPageRelay 的,后来用它做事。所以基本上 withLatestFrom 是我需要使用的东西。

class ViewModel2 {

   private func test() {
       let customViewModel: ViewModel1 = ViewModel1()

       customViewModel
           .output
           .hasItems
           .withLatestFrom(currentPageRelay) { ([=13=], ) }
           .map { (hasItems, page) -> (CustomClass2, Bool)? in 
               guard let property1 = page?.property1 else { return nil }
               return (property1, hasItems)
           }
           .unwrap()
           .drive(onNext: { (property1, hasItems) in 
               // do stuff
           }
           .disposed(by: disposeBag)
   }
}

Xcode完全输在withLatestFrom上。没有代码完成,它给出了以下编译错误: Expression type '(Bool, _)' is ambiguous without more context

我对此一无所知。我已经尝试了所有方法,在其下方的参数列表中提供了正确的 classes,以便它知道会发生什么等等,但到目前为止还没有成功。

我认为是因为 .withLatestFrom 要求它所操作的两种类型具有相同的可观察特征。所以两者都应该是 ObservableDriverSignal

如果你想在你的 viewModel 中保留你的 Driver Driver 你可以在 .hasItems:

之后添加一个 .asObservable()
class ViewModel2 {
    let currentPageRelay: BehaviorRelay<Page?> = BehaviorRelay<Page?>(value: nil)
    let disposeBag = DisposeBag()

    init() {
        // self.currentPageRelay = BehaviorRelay<Page?>(value: nil)
    }

    private func test() {
        let customViewModel: ViewModel1 = ViewModel1()

        customViewModel
            .output
            .hasItems
            .asObservable()
            .withLatestFrom(currentPageRelay) { ([=10=], ) }
            .map { (hasItems, page) -> (CustomClass2, Bool)? in
                guard let property1 = page?.property1 else { return nil }
                return (property1, hasItems)
            }
            .asDriver(onErrorJustReturn: nil)
            .drive(onNext: {
                guard let (property1, hasItems) = [=10=] else {
                    return
                }
                // do stuff
            })
            .disposed(by: disposeBag)
    }
}

或者在withLatestFrom(..)中的currentPageRelay中添加一个.asDriver()

customViewModel
    .output
    .hasItems
    .withLatestFrom(currentPageRelay.asDriver()) { ([=11=], ) }
    .map { (hasItems, page) -> (CustomClass2, Bool)? in
        guard let property1 = page?.property1 else { return nil }
        return (property1, hasItems)
    }
    .drive(onNext: {
        guard let (property1, hasItems) = [=11=] else {
            return
        }
        // do stuff
    })
    .disposed(by: disposeBag)