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

1.4k Views Asked by At

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?

3

There are 3 best solutions below

1
On BEST ANSWER

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

4
On

A class does not conform to NSObjectProtocol by default, that causes the error.

Change

protocol NavigationDelegate: NSObjectProtocol

to

protocol NavigationDelegate: class
0
On

Your NavigationDelegate uses a base protocol of NSObjectProtocol. This means that anything that conforms to NavigationDelegate must also conform to NSObjectProtocol. Change your class declaration to the following: class MyViewController: NSObject, NavigationDelegate.