Iva*_*van 4 selector swift swift3
谁能告诉我为什么此代码给出错误消息“'#selector'的参数不引用'@objc'方法,属性或初始化程序”吗?
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(updateTimer(until: 3)), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
功能如下:
func updateTimer(until endTime: Int) {
counter -= 1
timeLabel.text = String(counter)
if counter == endTime {
step += 1
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了什么:
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
}
}
Run Code Online (Sandbox Code Playgroud)
如果封闭类不继承表单NSObject,则必须将@objc属性添加到action方法。
| 归档时间: |
|
| 查看次数: |
8268 次 |
| 最近记录: |