gpt4 book ai didi

swift - 在Swift Combine中使用Publishers.debounce()的正确语法是什么?

转载 作者:行者123 更新时间:2023-12-01 07:29:56 26 4
gpt4 key购买 nike

在Apple的2019年WWDC视频Swift Combine in Practice中,他们演示了使用debounce发布者来降低消息发送速度。

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

但是,每当我尝试以类似方式使用它时,都会出现以下错误:

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


debounce()签名为:
public func debounce<S>(for dueTime: S.SchedulerTimeType.Stride, 
scheduler: S,
options: S.SchedulerOptions? = nil) ->
Publishers.Debounce<Self, S> where S : Scheduler
SchedulerTimeType.Stride似乎可以用数字初始化,但对我不起作用,或者我对Swift Generics的经验不足。

正确的称呼方式是什么?

编辑

this question...的副本

目前,搜索“Combine”之类的通用词颇具挑战性……

macOS 10.15,Xcode 11

最佳答案

documented 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)

关于swift - 在Swift Combine中使用Publishers.debounce()的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com