checkSelectorIsObservable case 验证

checkSelectorIsObservable case 验证

要触发第三个case需满足以下条件

  1. delegate method 返回值不为空

但根据消息转发策略

        override open func responds(to aSelector: Selector!) -> Bool {
            
            print("\(aSelector): responds = \(responds)")
            
            return super.responds(to: aSelector)
                || (self._forwardToDelegate?.responds(to: aSelector) ?? false)
                || (self.voidDelegateMethodsContain(aSelector) && self.hasObservers(selector: aSelector))
        }
        

永远无法触发第三个case, 而第二个case 与 if !(self._forwardToDelegate?.responds(to: selector) ?? true) 相违背。

所以经过分析 应该只有以下的情况会触发case


extension Reactive where Base: UIScrollView {


    public var viewForZooming: ControlEvent {

         let source = delegate.methodInvoked(#selector(UIScrollViewDelegate.viewForZooming(in:))).map { _ in }

        return ControlEvent(events: source)
    }



}



// 订阅 这样就可以满足 hasObservers

        _ = scrollView.rx.viewForZooming.subscribe { print($0) }
        // 设置delegate
        scrollView.delegate = self
        // 这个操作会触发 setForwardToDelegate ,从而触发 checkSelectorIsObservable
       _ = scrollView.rx.viewForZooming.subscribe { event in
            print("viewForZooming")
        }



结果

最终会触发第三个分支警告, 不过这样代码显然没有意义,这是一个异常分支,在编码的时候就应该避免它

你可能感兴趣的:(checkSelectorIsObservable case 验证)