Search code examples
uicollectionviewrx-swiftuicollectionreusableviewrx-cocoarxdatasources

RxSwift Cannot convert call result type '(_) -> Disposable' to expected type '(_) ->


I'm trying to add an headerView to a collectionView using RxSwift.

I get this error:

Cannot convert call result type '() -> Disposable' to expected type '() ->

at this line:

obsHeader.asObservable().bind(to: collectionView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)

I don't understand how to fix it. Any help?

I post here the rest of the code:

struct SectionItemObject {
    let collectionViewRecommendations: UICollectionView
    let items: [SFCardViewModelListOfCardsProtocol]
}

struct SectionOfItems {
    var items: [Item]
}

extension SectionOfItems: SectionModelType {

    typealias Item = SectionItemObject

    init(original: SectionOfItems, items: [Item]) {
        self = original
        self.items = items
    }

    init(items: [Item]?) {
        self.items = items ?? [Item]()
    }
}

And this is what I write in the method I call to observe.

let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfItems>(configureCell: { (datasource, collectionview, indexPath, i) -> UICollectionViewCell in
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "CardView", for: indexPath) as! CardView
        //                self.setCell(card:card,cell:cell)
        cell.lbTitle.text = "TEST"
        return cell
    }, configureSupplementaryView: { (datasource, collectionview, kind, indexPath) -> UICollectionReusableView in
        let section = collectionview.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "AddNewCardCollectionHeaderView", for: indexPath) as! AddNewCardCollectionHeaderView
        section.backgroundColor = UIColor.orange
        section.collectionViewRecommendations = self.collectionViewRecommendations
        return section
    } )

let item = SectionItemObject(collectionViewRecommendations: self.collectionViewRecommendations!, items: viewModelProtocol.searchedCards.value)
let obsHeader = Variable(SectionOfItems(items: [item]))

obsHeader.asObservable().bind(to: collectionView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)

Solution

  • I would bet that your obsHeader needs to be typed as Variable<[SectionOfItems]>

    So just make it

    let obsHeader = Variable([SectionOfItems(items: [item])])