我正在创建一个屏幕在黑白之间快速切换的应用程序。为此,我使用了一个 Timer
,
我的问题归结为无法在计时器声明中从相同范围 (changeBackgroundColor
) 调用函数。
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: changeBackgroundColor)
//Error: 'Cannot convert value of type '(ViewController) -> (Timer) -> Void' to expected argument type '(Timer) -> Void'
func changeBackgroundColor(timer: Timer) -> Void {
//change the color of the screen (not the issue here)
}
我以为我已经理解了闭包,但这里的作用域似乎有问题。
最佳答案
使用计时器“ block ”版本的全部意义在于避免必须创建单独的函数。
你应该这样写:
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
// change the color of the screen
}
关于ios - 从同一范围调用 block /闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44706105/