Delegate methods in child class sometimes not called with Swift 5 compiler

后端 未结 6 1090
野的像风
野的像风 2020-12-24 14:32

EDIT: As sunshinejr pointed out here, this has been fixed and will be released together with the next Xcode/Swift version.


I\'ve seen a lot of

6条回答
  •  难免孤独
    2020-12-24 14:53

    EDIT: As sunshinejr pointed out here, this has been fixed and will be released together with the next Xcode/Swift version.


    I've found the issue, here's how to reproduce it.

    class A: UIViewController, UIScrollViewDelegate {
        // ...does not implement 'scrollViewDidEndDecelerating'
    }
    
    class B: A {
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            // Will not be called!
        }
    }
    

    What does work:

    class A: UIViewController, UIScrollViewDelegate {
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            // Probably empty
        }
    }
    
    class B: A {
        override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            // Will be called!
        }
    }
    

    The compiler seems to think that a delegate method is not implemented if the base class did not implement it. If only the child class implements it, it can't find it.

    I still can't explain why this behaviour changed with Swift 5, but at least I've found a solution. Maybe someone can give further insights?

提交回复
热议问题