doesNotRecognizeSelector

doesNotRecognizeSelector

Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息,并通过editing this post阐明问题。

4年前关闭。



Improve this question




这是我的代码:
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

    最佳答案

    如果要动态处理无法识别的消息而不会崩溃,则应在Objective-C中覆盖forwardInvocation:(和methodSignatureForSelector:)(在Swift中不可用)。如果只需要将其转发给一个观察者,则一种更简单的方法是重写forwardingTargetForSelector:(在Swift中可用),但是如果您想执行多个操作,或者需要执行其他复杂的工作,则需要forwardInvocation:

    08-26 17:27