1
具体来说,当一个任务可以用选择器或不用选择器补充时,是否有任何(客观)理由偏好一种方式而不是另一种方式?是否有任何理由在Swift中使用选择器?
A)使用选择:
let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction()), userInfo: nil, repeats: true)
dynamic func timerAction() {
print("foo")
}
B)没有选择器:
例如,一个NSTimer可以以两种方式运行上的间隔的方法
let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
self.timerAction()
}
func timerAction() {
print("foo")
}
两个版本是完全有效的,那么是否有任何(客观)理由相互偏好?
块版本是一个iOS10 +方法只适用于您的情况。如果您的目标是iOS9,则无法使用它。 – Larme
@Larme有趣。两者都在macOS 10.12上工作,但我不能早些讲话。 –
@BrandonBradley只需查看文档。在MacOS 10.12和iOS 10.0中添加了块版本的Timer.scheduledTimer。 – rmaddy