Swift protocol in Objective-C class

前端 未结 6 480
灰色年华
灰色年华 2021-02-02 06:39

I wrote SearcherProtocol in Swift and need to implement an Objective-C class FileSearcher which has to use this protocol.

So I tried this:

6条回答
  •  情话喂你
    2021-02-02 06:58

    I know this was a long time ago, but I just struggled with the same problem when adding a protocol to my Swift code, and it wasn't being added to the -Swift.h header file, hence "Cannot find protocol declaration"

    The problem was my protocol wasn't marked as Public. I changed my protocol from this:

    @objc protocol MyProtocol { //etc.... }
    

    to this:

    @objc public protocol MyProtocol { //etc.... }
    

    I'm still not entirely sure why I need 'Public' but nobody else seems to, but hey it works...

提交回复
热议问题