RxSwift filter Variable array

Viewed 11205

Hi I am trying to get an understanding of the RxSwift library to write better, functional code.

Currently I am stuck at a very basic problem. Lets say I got this variables of type Variable<[CiteModel?]>:

var allCites: Variable<[CiteModel?]> = Variable([])
var shownCites: Variable<[CiteModel?]> = Variable([])

Now I want to filter all cites from allCites array which contain a specific text and add them to shownCites.

This is what I have tried but it does not compile because inside my filter block $0 is [CiteModel?] not CiteModel? what I would expect. Could you explain to me what I did wrong ?

private func filterCitesByQuery(query: String) {
    self.shownCites = self.allCites.asObservable().filter {
        $0?.cite.containsString(query)
    }
}

Error when executing the above code:

Cannot assign value of type 'Observable<[CiteModel?]>' (aka 'Observable<Array<Optional<CiteModel>>>') to type 'Variable<[CiteModel?]>' (aka 'Variable<Array<Optional<CiteModel>>>')
3 Answers
Related