Search code examples
iosswiftswift-protocolsprotocol-oriented

Default protocol implementation causes 'does not conform to protocol' error


I am trying to add a default implementation to one of my delegate methods. However, after adding the default implementation and removing the method from the class that implements the protocol, I get does not conform to protocol error. It works in a playground.

protocol NavigationDelegate: NSObjectProtocol {
    func didSetToolbarVisible(_ isVisible: Bool)
}
extension NavigationDelegate {
    func didSetToolbarVisible(_ isVisible: Bool) {
        print("Default implementation")
    }
}
class MyViewController: NavigationDelegate {
    // 'does not conform to protocol' error
}

What am I missing?


Solution

  • Solved it! My NavigationDelegate and its extension were in a different target than the one that MyViewController belongs to. Simply moving the extension to the same target worked.

    Hope this helps someone in the future 🤞