Why Swift cannot see Swift protocol conformance of ObjC class?

I have this Swift protocol:

@objc public protocol ViewControllerAPI {
    func foo()
}

And a ObjC class adopting the protocol:

@interface ViewController : UIViewController <ViewControllerAPI>

@end

In anther Swift file, when I try to call the protocol method foo, Xcode complains ViewController has no member foo. Why?

class Presenter {
    
    func present() {
        ViewController().foo() // Value of type 'ViewController' has no member 'foo'
    }
    
}

I made a sample project with this setup and I was able to fix the build error by adding the method stub -(void)foo; to the ViewController's header file. Another approach you can take is to make a swift extension on the view controller to conform to the protocol, that seems to get picked up just fine by the presenter.

As to why this is, I'm not entirely sure. The protocol conformance is visible to objective-c, it seems like Swift can only see methods explicitly added to the header files and doesn't infer conformance. That makes sense for optional protocol methods but it seems like it could correctly assume conformance for required methods.

1 Like

Right, I know explicitly declaring the method again in header is a workaround. I'm just wondering why protocol conformance is not enough. @Joe_Groff is it a bug?

Yeah, this looks like a bug.

1 Like