我有這樣的代碼: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
的擴展實現我有同樣的問題。你有沒有解決方案?我現在只是在我的班級實施功能 –