这是我的代码:
class RNObserverManager: NSObject {
var observers:Set<NSObject> = []
override func doesNotRecognizeSelector(aSelector: Selector){
var isRecognized = false
for observer in observers{
if observer.respondsToSelector(aSelector){
observer.performSelector(aSelector)
isRecognized = true
}
}
if !isRecognized{
super.doesNotRecognizeSelector(aSelector)
}
}
}
无论我是否致电super.doesNotRecognizeSelector(aSelector)
,应用程序都会崩溃
我试图覆盖doesNotRecognizeSelector()
,以便我的自定义类可以动态响应某些选择器。
1.如果我致电super.doesNotRecognizeSelector(aSelector)
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [ObserverTrampoline.RNObserverManager doSomething]:无法识别的选择器发送到实例0x7fdfebdb42e0'
super.doesNotRecognizeSelector(aSelector)
EXC_BREAKPOINT
答案 0 :(得分:1)
如果要在不崩溃的情况下动态处理无法识别的消息,则应覆盖Objective-C中的forwardInvocation:
(和methodSignatureForSelector:
)(它们在Swift中不可用)。如果你只需要将它转发给一个观察者,那么更简单的方法是覆盖forwardingTargetForSelector:
(在Swift中可用),但是如果你想做多个,或者需要做其他复杂的东西,你需要forwardInvocation:
。