Nerethar Nerethar - 3 months ago 10
Objective-C Question

Make a objective C method that uses a swift protocol as parameter public accesable

I have an objective-c method that uses a swift protocol as a parameter type. The signature looks like

+ (void) my_ObjC_method: (id<my_Swift_protocol>) parameter_name;


I generally know how to make swift protocols accessible to objective C.
I implemented it this way:

@objC protocol my_Swift_protocol : class {
//...methods go here
}


My problem is that I want to make
my_ObjC_method
public accessible. That would normally be done by simply adding it to the header file in the
@interface
part. Sadly, this requires to import the bridging header to the .h file, so that the .h file knows my swift protocol and recognizes it as a valid type. But you can't import the automatically generated bridging header into other header files.

What would be a good approach to solve or work around this problem?

Answer

Maybe you can use @protocol directive as a forward declaration.

MyClass.h:

#import <Foundation/Foundation.h>

@protocol my_Swift_protocol;

@interface MyClass : NSObject
+ (void) my_ObjC_method: (id<my_Swift_protocol>) parameter_name;

@end