本文介绍了定时器,#selector 解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个计时器,所以我使用了这个代码:
I need a timer so I used this code:
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(generalKnowledge.method), userInfo: nil, repeats: true)但我不理解#selector.我试了好多次都没用.
But I do not understand the #selector. I tried multiple times but it doesn't work.
推荐答案selector() 是您在每次设置 timeInterval 时添加您希望它调用的函数的地方.在您的示例中,它是每秒.
selector() is where you'd add in the function that you want it to call every timeInterval you set. In your example it's every second.
请记住,在 Swift 4 及更高版本中,如果您想在这样的选择器中调用它,则需要在函数前添加 @objc:
Do bare in mind that in Swift 4 and above, you need to add @objc before a function if you want to call it in a selector like so:
@objc func handleEverySecond() { print("Hello world!") } timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleEverySecond), userInfo: nil, repeats: true)更多推荐
定时器,#selector 解释
发布评论