12

In Apple's 2019 WWDC video Swift Combine in Practice, they demonstrate using a debounce publisher to slow down the rate of messages.

return $username
  .debounce(for: 0.5, scheduler: RunLoop.main)
  .removeDuplicates()
  .eraseToAnyPublisher()

However, anytime I attempt to use it in a similar fashion, I get the following error:

Cannot invoke 'debounce' with an argument list of type '(for: Double, scheduler: RunLoop)'

The debounce() signature is:

public func debounce<S>(for dueTime: S.SchedulerTimeType.Stride, 
                          scheduler: S,
                            options: S.SchedulerOptions? = nil) -> 
                                    Publishers.Debounce<Self, S> where S : Scheduler

SchedulerTimeType.Stride appears to be initializable with a numeric but it's not working for me or my inexperience with Swift Generics is on display.

What is the correct way to call this?

Edit

Duplicate of this question...

Searching for generic words like "Combine" is, for now, rather challenging...

macOS 10.15, Xcode 11

4

1 回答 1

22

记录在案debounce<S>运算符接受S.SchedulerTimeType.Stride如下所示的类型:

let sub = NotificationCenter.default
    .publisher(for: NSControl.textDidChangeNotification, object: filterField)
    .debounce(for: .milliseconds(500), scheduler: RunLoop.main)
    .subscribe(on: RunLoop.main)
    .assign(to:\MyViewModel.filterString, on: myViewModel)
于 2019-06-20T16:02:30.907 回答