Protocol methods in a class extension are not called under specific conditions

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:24:27

问题


I encountered a weird behavior. The best way I can put it is … Not overridden protocol methods in a class extension are not called while the superclass already conforms to the protocol (via extension). However this happens only while it's build with the release build configuration.

class A: UIViewController {}

extension A: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("scrollViewDidScroll in superclass")
    }
}

class B: A {
    // A tableView (and its data source and delegate) is set here…
}

extension B: UITableViewDelegate {
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        super.scrollViewDidScroll(scrollView)
        print("scrollViewDidScroll in subclass")
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("scrollViewDidEndDragging")
    }
}

The output:

scrollViewDidScroll in superclass

scrollViewDidScroll in subclass

scrollViewDidEndDragging

however if I build it with the release build configuration, the output is

scrollViewDidScroll in superclass

scrollViewDidScroll in subclass

I can solve the problem if I don't use the extension for protocol conformance approach in the class B and just use the regular way instead (put the methods that implement a protocol into the class).

The question is … how come?

来源:https://stackoverflow.com/questions/56645685/protocol-methods-in-a-class-extension-are-not-called-under-specific-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!