Protocol methods in a class extension are not call

2019-08-17 05:03发布

问题:

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?