我有这样的代码:Swift选择器到协议功能?
protocol FooP {
...
}
extension FooP {
func doFoo() {
print("foo")
}
func doFoo(timer: NSTimer) {
doFoo()
}
}
class A : NSObject, UITableViewDataSource, FooP {
var timer : NSTimer?
...
func startUpdating() {
timer = NSTimer.scheduledTimerWithTimeInterval(
1.0,
target: self,
selector: Selector("doFoo:"),
userInfo: nil,
repeats: true
)
}
}
不幸的是它崩溃的时候,我开始计时程序崩溃与
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[xyz.A doFoo:]: unrecognized selector sent to instance 0x7fb2041c4ac0'
我怎样才能使它发挥作用(我想保持内部协议实现doFoo的) ?
如果我将doFoo移入A类定义一切正常,但正如我所说我想在协议内部实现此功能。
换句话说,我需要选择,说
"Hey I point to function named "doFoo" that is implemented as extension to FooP"
现在选择似乎是说
"Hey I point to function named "doFoo" that is implemented in A class"
可能重复的[“无法识别的选择器发送到实例”在Swift](http://stackoverflow.com/questions/24094620/unrecognized-selector-sent-to-instance-in-swift) – Matheno
不是我的问题是,计时器看不到功能作为协议 – Pikacz
的扩展实现我有同样的问题。你有没有解决方案?我现在只是在我的班级实施功能 –