【问题标题】:How to set RxTimeInterval for debounce in RxSwift?如何在 RxSwift 中设置 RxTimeInterval 以防抖动?
【发布时间】:2019-06-08 13:21:47
【问题描述】:

无法在 rxswift 中设置去抖动的 Rxtimeinterval。我的代码如下。我收到此错误消息“无法将 'Double' 类型的值转换为预期的参数类型 'RxTimeInterval' (aka 'DispatchTimeInterval')”

searchBar
    .rx.text // Observable property thanks to RxCocoa
    .orEmpty // Make it non-optional
    .debounce(0.5, scheduler: MainScheduler.instance) // Wait 0.5 for changes.
    .distinctUntilChanged() // If they didn't occur, check if the new value is the same as old.
    .filter { !$0.isEmpty }

错误信息:

“无法将“Double”类型的值转换为预期的参数类型“RxTimeInterval”(又名“DispatchTimeInterval”)”

【问题讨论】:

    标签: ios swift xcode rx-swift


    【解决方案1】:
    searchBar
        .rx.text // Observable property thanks to RxCocoa
        .orEmpty // Make it non-optional
        .debounce(.milliseconds(500), scheduler: MainScheduler.instance) // Wait 0.5 for changes.
        .distinctUntilChanged() // If they didn't occur, check if the new value is the same as old.
        .filter { !$0.isEmpty }
    

    【讨论】:

      【解决方案2】:

      更改这一行:

      .debounce(0.5, scheduler: MainScheduler.instance)
      

      至此行:

      .debounce(RxTimeInterval.milliseconds(500), scheduler: MainScheduler.instance)
      

      【讨论】:

        【解决方案3】:

        “无法将“Double”类型的值转换为预期的参数类型“RxTimeInterval”(又名“DispatchTimeInterval”)”

        我遇到了同样的错误,我只是添加了这个:

        .debounce(RxTimeInterval.milliseconds(500), scheduler: MainScheduler.instance)

        而不是这个 .debounce(0.5, scheduler: MainScheduler.instance)


        只需确保您在 milliseconds(<value>) 中的值是一个 int

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-28
          • 2019-03-15
          • 2020-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-27
          相关资源
          最近更新 更多