如何在RxSwift中将RxTimeInterval设置为防抖动?

时间:2019-06-08 13:21:47

标签: ios swift xcode rx-swift

无法为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'(aka'DispatchTimeInterval')”

3 个答案:

答案 0 :(得分: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 }

答案 1 :(得分:1)

  

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

我遇到了相同的错误,我刚刚添加了这个错误:

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

代替此 .debounce(0.5, scheduler: MainScheduler.instance)


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

答案 2 :(得分:0)

更改此行:

.debounce(RxTimeInterval(0.5), scheduler: MainScheduler.instance)