【发布时间】:2019-04-07 18:15:25
【问题描述】:
这里我有一个简单的类,它每秒打印几次“测试”:
import Foundation
class TestClass {
static var count = 0
static var timer = Timer()
init() {
TestClass.start()
}
static func start() {
TestClass.timer = Timer.scheduledTimer(timeInterval: 1.0/5.0, target: self, selector: #selector(printTest), userInfo: nil, repeats: true)
}
@objc func printTest() {
print(count)
count += 1
}
}
我将函数 start 设为变量 timer static 以便稍后我可以从另一个类启动和停止计时器
在我的主 ViewController 文件中,我只需创建一个新的 TestClass 并说:
let _ = TestClass()
然后我尝试通过以下方式停止和启动计时器:
if /*something*/ {
TestClass.timer.invalidate()
}
if /*something else*/ {
TestClass.start()
}
但是,当我运行项目时,我在控制台中收到多个错误:
NSForwarding: warning: object 0x10eaab580 of class 'DeleteMe.TestClass' does not implement methodSignatureForSelector: -- trouble ahead
和
Unrecognized selector +[DeleteMe.TestClass printTest]
我必须在课堂上修改什么来解决错误吗?
【问题讨论】: