如何在 RxSwift 中为去抖设置 RxTimeInterval?

How to set RxTimeInterval for debounce in RxSwift?

无法在 rxswift 中为去抖设置 Rxtimeinterval。我的代码如下。我收到此错误消息 "Cannot convert value of type 'Double' to expected argument type '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 { ![=10=].isEmpty }

错误信息:

"Cannot convert value of type 'Double' to expected argument type 'RxTimeInterval' (aka 'DispatchTimeInterval')"

更改此行:

.debounce(0.5, scheduler: MainScheduler.instance)

到这一行:

.debounce(RxTimeInterval.milliseconds(500), scheduler: MainScheduler.instance)
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 { ![=10=].isEmpty }

"Cannot convert value of type 'Double' to expected argument type 'RxTimeInterval' (aka 'DispatchTimeInterval')"

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

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

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


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