Importing a Swift protocol in Objective-C class
It is not possible to import the Xcode generated Swift header in objC header files.
So, since you want to use Swift code in an objC header file, you will need to "forward declare" the classes and protocols you want to use in the objC header file, like this:
@protocol AnalyticProtocol;
You can now use the protocol in your objC class declaration:
@interface AnalyticFactory : NSObject
{
Class<AnalyticProtocol> _analyticProtocolClass; // The type of the analytic class currently used.
}
In your implementation file (the objC .m file), you can import the Xcode generated Swift header ("ProductModuleName-Swift.h") file and the correct implementation AnalyticProtocol
will now be known to the compiler.
This is also described in official Importing Swift into Objective-C
Note that XCode will give a warning in the objC header file when you use the forward declared protocol ("Cannot find protocol definition for 'AnalyticProtocol'), but this is can be ignored - the implementation will be found at compile time.
You need to add the @objc
attribute to your Swift protocol like so:
@objc protocol AnalyticProtocol {
}