0
我是新來RxSwift和我遇到的下一個形勢下應運而生:如何將一個類型的可觀察數組轉換爲另一種類型的可觀測陣列RxSwift
self.viewModel!.country
.flatMapLatest { country -> Observable<City> in
country!.cities!.toObservable()
}
.map { city -> SectionModel<String, String> in
SectionModel(model: city.name!, items: city.locations!)
}
.toArray()
.bindTo(self.tableView.rx_itemsWithDataSource(dataSource))
.addDisposableTo(self.disposeBag)
我看來,像指定者()什麼都不做我不知道爲什麼。另一方面,這個代碼做我想要的。我想知道爲什麼以前的代碼不能以相同的方式工作:
self.viewModel!.country
.flatMapLatest { country -> Observable<[SectionModel<String, String>]> in
var models = [SectionModel<String, String>]()
for city in country!.cities! {
models.append(SectionModel(model: city.name!, items: city.locations!))
}
return Observable.just(models)
}
.bindTo(self.tableView.rx_itemsWithDataSource(dataSource))
.addDisposableTo(self.disposeBag)
在此先感謝。
編輯:
視圖模型實現如下:
class LocationViewModel {
let country = BehaviorSubject<Country?>(value: nil)
}
國家有一個屬性「城市」,這是城市的數組。如果我在toArray()之前和之後放置debug(),我得到2個訂閱,每個調試()一個,每個數組項僅有一個next事件toArray(),所以你必須是正確的。調試()。
但是,爲什麼它沒有完成?
我不知道爲什麼它不會完成,給你說什麼。如果您可以發佈能夠執行的代碼以顯示問題,我將能夠進一步提供幫助。 – solidcell