定时器,#selector 解释

编程入门 行业动态 更新时间:2024-10-11 05:28:42
本文介绍了定时器,#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 解释

本文发布于:2023-06-03 09:51:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定时器   selector

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!