如何在带有RxSwift的驱动程序上使用flatMapLatest

如何在带有RxSwift的驱动程序上使用flatMapLatest,swift,reactive-programming,rx-swift,rx-cocoa,Swift,Reactive Programming,Rx Swift,Rx Cocoa,每当我的用户位置发生变化时,我都试图从网络中获取一些数据 struct CityService { private init() {} static let shared = CityService() lazy var nearbyCities: Driver<[City]> = { return GeolocationService.instance.location .flatMapLatest({ coordinate in

每当我的用户位置发生变化时,我都试图从网络中获取一些数据

struct CityService {
  private init() {}

  static let shared = CityService()

  lazy var nearbyCities: Driver<[City]> = {
    return GeolocationService.instance.location
      .flatMapLatest({ coordinate in
        let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
        return CityService.shared.fetchNearbyCitiesFor(location)
      }).asDriver(onErrorJustReturn: [])
  }()

  func fetchNearbyCitiesFor(_ location: CLLocation) -> Observable<[City]> {
    return Observable.create { observer in
      let disposable = Disposables.create()

      // Mock a fetch from the network:
      let cities = [City(name: "Amsterdam"), City(name: "Berlin")]
      observer.onNext(cities)
      observer.on(.completed)

      return disposable
    }
  }
}

class GeolocationService {
  static let instance = GeolocationService()
  private (set) var location: Driver<CLLocationCoordinate2D>
}
// from: https://github.com/ReactiveX/RxSwift/blob/master/RxExample/RxExample/Services/GeolocationService.swift

struct City {
  let name: String
}
struct CityService{
私有init(){}
静态let shared=CityService()
lazy var nearbyCities:驱动程序={
返回GeolocationService.instance.location
.flatMapLatest({坐标在
let location=CLLocation(纬度:坐标。纬度,经度:坐标。经度)
return CityService.shared.fetchNearbyCitiesFor(位置)
}).asDriver(onErrorJustReturn:[])
}()
func fetchNearbyCitiesFor(u位置:CLLocation)->可观察{
返回可观察的。在中创建{observer
让一次性的=一次性的。创建()
//模拟从网络获取:
let cities=[城市(名称:“阿姆斯特丹”)、城市(名称:“柏林”)]
onNext观察员(城市)
观察员。在(.completed)
一次性退货
}
}
}
类别地理定位服务{
静态let实例=GeolocationService()
专用(设置)变量位置:驱动程序
}
//发件人:https://github.com/ReactiveX/RxSwift/blob/master/RxExample/RxExample/Services/GeolocationService.swift
结构城市{
let name:String
}
但是,由于以下原因,这不是编译:

Cannot convert value of type 'SharedSequence<DriverSharingStrategy, [Any]>' to specified type 'Driver<[City]>'
(aka 'SharedSequence<DriverSharingStrategy, Array<City>>')
无法将“SharedSequence”类型的值转换为指定的“Driver”类型
(又名“共享序列”)
我还尝试添加一些类型提示以获得更好的错误:

lazy var nearbyCities: Driver<[City]> = {
  return GeolocationService.shared.location
  .flatMapLatest({ coordinate -> Observable<[City]> in
    let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
    let nearbyCities: Observable<[City]> = CityService.shared.fetchNearbyCitiesFor(location)
    return nearbyCities.catch
  }).asDriver(onErrorJustReturn: [City]())
}()
lazy-var-nearbyCities:Driver={
返回GeolocationService.shared.location
.flatMapLatest({坐标->在
let location=CLLocation(纬度:坐标。纬度,经度:坐标。经度)
let nearbyCities:Observable=CityService.shared.fetchNearbyCitiesFor(位置)
返回附近城市
}).asDriver(onErrorJustReturn:[City]())
}()
但这给了我的是:

Cannot convert value of type '(_) -> Observable<[City]>' to expected argument type '(CLLocationCoordinate2D) -> SharedSequence<_, _>'
无法将类型为“()->Observable”的值转换为预期的参数类型“(CLLocationCoordinate2D)->SharedSequence”

我做错了什么?

你把
.asDriver
调用放错地方了

lazy var nearbyCities: Driver<[City]> = {
    return GeolocationService.instance.location
        .flatMapLatest({ (coordinate) -> Driver<[City]> in
            let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
            return CityService.shared.fetchNearbyCitiesFor(location)
                .asDriver(onErrorJustReturn: [])
        })
}()
lazy-var-nearbyCities:Driver={
返回GeolocationService.instance.location
.flatMapLatest({(坐标)->中的驱动程序
let location=CLLocation(纬度:坐标。纬度,经度:坐标。经度)
return CityService.shared.fetchNearbyCitiesFor(位置)
.asDriver(onErrorJustReturn:[])
})
}()

啊!是的,终于。谢谢。不过,我还是不能对司机掉以轻心。。有没有最新RxSwift的好资源?