简体   繁体   English

如何在Swift 3中为带有可选参数的函数创建选择器?

[英]How to create selector for function with optional parameter in Swift 3?

I need to create a timer like this: 我需要创建一个这样的计时器:

timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(dismissNotification(completion:)), userInfo: nil, repeats: false)

func dismissNotification(completion: (() -> ())? = nil) { ... }

but it crashes without displaying any reason. 但它崩溃而没有显示任何原因。 Why it happens? 为什么会发生? And how to workaround this? 以及如何解决这个问题?

(NS)Timer allows only two types of actions: (NS)Timer仅允许两种类型的操作:

Without a parameter 没有参数

  • func timerFireMethod()

With a single parameter passing the affected (NS)Timer instance 通过单个参数传递受影响的(NS)Timer实例

  • func timerFireMethod(_ timer : Timer)

However in iOS 10 / macOS 10.12 you can use a new API with a closure 但是在iOS 10 / macOS 10.12中,您可以使用带闭包的新API

class func scheduledTimer(withTimeInterval interval: TimeInterval, 
                                            repeats: Bool, 
                                              block: @escaping (Timer) -> Void) -> Timer

The function crashes because the timer passes itself to the selector method. 该函数崩溃是因为计时器将自身传递给选择器方法。 And the selector method expects an anonymous function. 选择器方法需要一个匿名函数。

One thing you can do is this: 您可以做的一件事是:

timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(dismissNotification(timer:)), userInfo: nil, repeats: false)

func dismissNotification(timer: Timer) {
    //call dismissNotification(completion) here
}
func dismissNotification(completion: (() -> ())? = nil) { ... }

Also, you can pass additional data to the dismissNotification(timer: Timer) function using the Timer.userInfo property. 另外,您可以使用Timer.userInfo属性将其他数据传递给dismissNotification(timer: Timer)函数。

The completion handler needs to have the signature 完成处理程序需要具有签名

 func dismissNotification(completion: Timer) { ... }

It is also possible to omit the Timer parameter: 也可以忽略Timer参数:

 func dismissNotification() { ... }

Dispatch also provides similar functionality allowing the code to be executed on an arbitrary queue: Dispatch还提供了类似的功能,允许在任意队列上执行代码:

// It's necessary to keep a reference to timer
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
timer.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTime.seconds(2))
timer.setEventHandler() { ... } 
timer.resume()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM