当使用泛型时,rxswifttoArray对我不起作用:

struct SaveModelsCommand<M> where M:Model {

    let models:[M]

    func create() -> Observable<[M]> {

        let cloudKitRecords:[CKRecord] = models.map({
            // convert models to CKRecords
            ...
        })

        return SaveRecordsCommand(cloudKitRecords)
            .createObservable()
            .flatMap({ savedRecords in
                // convert array to multiple emissions so we can iterate it
                return Observable.from(savedRecords)
            })
            .flatMap({ (record:CKRecord) -> M in
                // convert CKRecord back to a model (aka M)
                ... create model (e.g. Member) ...
                return model
            })
            // convert back to a single emission (array)
            .toArray() // <<<<< ERROR
    }
}

错误如下:
无法将“observate”类型(也称为“observate>”)的返回表达式转换为“observate”类型(也称为“observate>”)
我在返回类型中看到的唯一区别是M.EvsM
有什么想法吗?

最佳答案

flatMap期望闭包返回一个Observable<M>,而不仅仅是一个普通的M

.flatMap({ (record:CKRecord) -> Observable<M> in
    // convert CKRecord back to a model (aka M)
    //... create model (e.g. Member) ...
    return Observable.just(model)
})

或者,您可以使用map并返回M
.map({ (record:CKRecord) -> M in
    // convert CKRecord back to a model (aka M)
    //... create model (e.g. Member) ...
    return model
})

10-06 15:01