RxSwift toArray 不使用通用参数编译
RxSwift toArray does not compile with generic parameter
RxSwift toArray
在使用泛型时对我不起作用:
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
}
}
这是错误:
Cannot convert return expression of type 'Observable<[M.E]>' (aka 'Observable>') to return type 'Observable<[M]>' (aka 'Observable>')
我在 return 类型中看到的唯一区别是 M.E
与 M
。
有什么想法吗?
flatMap
期望闭包为 return 和 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
,只使用 return 和 M
:
.map({ (record:CKRecord) -> M in
// convert CKRecord back to a model (aka M)
//... create model (e.g. Member) ...
return model
})
RxSwift toArray
在使用泛型时对我不起作用:
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
}
}
这是错误:
Cannot convert return expression of type 'Observable<[M.E]>' (aka 'Observable>') to return type 'Observable<[M]>' (aka 'Observable>')
我在 return 类型中看到的唯一区别是 M.E
与 M
。
有什么想法吗?
flatMap
期望闭包为 return 和 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
,只使用 return 和 M
:
.map({ (record:CKRecord) -> M in
// convert CKRecord back to a model (aka M)
//... create model (e.g. Member) ...
return model
})