【发布时间】:2016-02-09 12:20:37
【问题描述】:
协议扩展和 addTarget 崩溃并显示消息:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[Test.UIButton touchDown:]: unrecognized selector sent to instance 0x157eee8e0'
touchDown函数无法识别的问题在哪里?
protocol MyButtonProtocol {
var holdTimer: NSTimer? { get set }
}
extension MyButtonProtocol where Self: UIButton {
func enable() {
addTarget(self, action: "touchDown:", forControlEvents: UIControlEvents.TouchDown)
}
mutating func touchDown(sender: UIButton) {
print("Touch down!")
holdTimer = NSTimer(timeInterval: 1, target: self, selector: Selector("didTimeOut"), userInfo: nil, repeats: true)
}
}
// Usage:
let button = UIButton()
button.enable()
【问题讨论】:
-
UIButton不符合MyButtonProtocol? -
这看起来像一个错误。也在这里:stackoverflow.com/questions/31060365/… 或者它是一个功能? stackoverflow.com/questions/31431753/…
-
每个人都对此感到困惑。这是另一个:bugs.swift.org/browse/SR-544。因此,来自协议扩展的方法不能用于 Objective-C 调用,就像任何使用“选择器”的东西一样。感谢您的努力,Ramis,这非常有帮助。