gpt4 book ai didi

swift - 遇到 NSTimer (Swift) 的问题

转载 作者:IT王子 更新时间:2023-10-29 05:33:43 24 4
gpt4 key购买 nike

--使用更新的信息进行编辑--

我想做的是使用 NSTimer.scheduledTimerWithTimeInterval 方法每五秒调用一个名为 timerFunc 的函数,问题似乎是在运行时,我得到了错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-      
[Animation.ViewController timerFunc:]: unrecognized selector sent to instance 0x7fe548d66040'

在输出日志中。我一直在查找其他人的 NSTimers 无济于事,我看到很多选择器都是 selector: Selector("timerFunc:") 而不是 selector: Selector("timerFunc") 但是,这两种方式都会出错。另一件事是 timerFunc 函数和 NSTimer 都在 viewDidLoad 内部,这有什么问题吗?非常感谢任何对此问题的见解,感谢阅读。

下面的timerFunc

func timerFunc(){

println("Timer")
}

下方的 NSTimer

NSTimer.scheduledTimerWithTimeInterval(
5.0,
target: self,
selector: Selector("timerFunc"),
userInfo: nil,
repeats: true)

最佳答案

计时器不能与私有(private)方法回调一起使用。此外,确保您的类继承自 NSObject。纯 Swift 类将不起作用。

可在此处找到更多信息:https://github.com/NxSoftware/NxSwiftTimer

关于swift - 遇到 NSTimer (Swift) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25838752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com