“#selector”的参数不引用“@objc”方法、属性 或初始化程序
Argument of '#selector' does not refer to an '@objc' method, property or initializer
谁能告诉我为什么这段代码给出错误信息"Argument of '#selector' does not refer to an '@objc' method, property or initializer"?
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(until: 3)), userInfo: nil, repeats: true)
函数如下:
func updateTimer(until endTime: Int) {
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}
我尝试过的:
1.在函数前添加@objc。
目标/操作方法的选择器必须在没有参数的情况下声明,或者有一个参数传递受影响的对象。
在 Timer
的情况下使用 userInfo
参数传递数据。
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(_:)), userInfo: 3, repeats: true)
func updateTimer(_ timer: Timer) {
let endTime = timer.userInfo as! Int
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}
如果封闭 class 不继承形式 NSObject
您必须将 @objc
属性添加到操作方法。
谁能告诉我为什么这段代码给出错误信息"Argument of '#selector' does not refer to an '@objc' method, property or initializer"?
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(until: 3)), userInfo: nil, repeats: true)
函数如下:
func updateTimer(until endTime: Int) {
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}
我尝试过的:
1.在函数前添加@objc。
目标/操作方法的选择器必须在没有参数的情况下声明,或者有一个参数传递受影响的对象。
在 Timer
的情况下使用 userInfo
参数传递数据。
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(_:)), userInfo: 3, repeats: true)
func updateTimer(_ timer: Timer) {
let endTime = timer.userInfo as! Int
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}
如果封闭 class 不继承形式 NSObject
您必须将 @objc
属性添加到操作方法。