我试图在我的Swift 3模型中创建Timer
的实例。简而言之,我的代码看起来像这样:
class Foo {
var timer = Timer()
func ping() {
print("Pong!")
}
func startPing() {
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(self.ping), userInfo: nil, repeats: true)
}
}
这出错:1: Argument of '#selector' refers to instance method 'ping()' that is not exposed to Objective-C
当我将timer
初始化更改为:timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: Selector("ping"), userInfo: nil, repeats: true)
时,我得到没有使用Objective-C选择器声明的方法' ping'`
这里有什么问题?