Swift protocol in Objective-C class

前端 未结 6 557
灰色年华
灰色年华 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 07:04

    When you have

    #import "moduleName-Swift.h" 
    

    in the .h file that you want to be a delegate, and you have that .h file also in the bridging headers file, there's a circular reference that causes the moduleName-Swift.h to fail compilation. for @james_alvarez's test project, it's probably working because you don't need to include TestObjClass.h into the bridging header.

    The best way for me to combine objc files that need to be the delegate for a class written in swift, but that also needs to be included in the bridging header so other swift files can access this objc class, is to create a separate protocol file in objc:

    MyProtocol.h:

    @protocol MyDelegate 
    
    -(void)didDoThis;
    -(void)didDoThat;
    
    @end
    

    ViewController.h:

    #import "MyProtocol.h"
    
    @interface ViewController : UIViewController 
    

    MyProject-Bridging-Header.h

    #import "MyProtocol.h"
    #import "ViewController.h"
    

提交回复
热议问题