简体   繁体   中英

Cannot call value of non function type error in RxSwift?

Hello i am quite newbie in RxSwift and Swift.

I am getting following error "Cannot call value of non function type ((ResponseTestData) -> Swift.Void)?"

My first question is why i am getting this error when i call onResult(result!) ???

func getTestList(perPage : String, pageNum : String, onResult: ((ResponseTestData) -> Swift.Void)? = nil, onFail: ((Error) -> Swift.Void)? = nil, disposeBag : DisposeBag)
{
    let tmpRx : RxManager
    tmpRx = RxManager()

    tmpRx.fetchTestData(perPage : perPage, pageNum : pageNum)
        .debug()
        .observeOn(MainScheduler.instance)
        .subscribeOn(MainScheduler.instance)
        .subscribe(
            onNext:
            {
                (result : ResponseTestData)->() in
                onResult(result!)
                print(result.result!)
            },
            onError:
            {
                _ in
                print("")
            },
            onCompleted:
            {
                print("")
            }

        )
        .disposed(by: disposeBag)

First, onResult is an Optional, so you need to use optional chaining. Therefore, onResult(result!) becomes onResult?(result!)

Second, if your fetchTestData returns an Observable<ResponseTestData> , then you need to remove the ! from result .

So I would expect it to look like this:

.subscribe(onNext: { (result : ResponseTestData) -> Void in
        onResult?(result)
        print(result)
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM