Importing a Swift protocol in Objective-C class

Jean Lebrument picture Jean Lebrument · Jun 6, 2014 · Viewed 59.2k times · Source

I try to import a Swift Protocol named AnalyticProtocol into an Objective-C class named AnalyticFactory.

protocol AnalyticProtocol
{

}

I'm starting from an existing Objective-C project (I didn't create a new Swift project with xCode and I didn't found how configure my Objective-C project to be a Swift project in xCode 6).

In my Swift file I included the .h file named MyProjectName-Swift.h but the compiler return me an error telling me that it doesn't exist. So, I created a .h file named MyProjectName-Swift.h which is actually empty (I don't know what I should put inside).

In the Apple documentation they said that I have to include my .h file named MyProjectName-Swift.h in my .m file. But I need to include it not into my .mfile but into my .h. Does this can be problematic?

When I try to compile I've got this error: :0: error: xxxAnalyticFactory.h:39: cannot find protocol declaration for 'AnalyticProtocol'

And the incriminated code:

@interface AnalyticFactory : NSObject
{
    Class<AnalyticProtocol> _analyticProtocolClass; // The type of the analytic class currently used.
}

I think I don't understand well how can I import a Swift protocol into an Objective-C class.

Does anyone see an error in what I'm doing?

Answer

Jamie Forrest picture Jamie Forrest · Jun 6, 2014

You need to add the @objc attribute to your Swift protocol like so:

@objc protocol AnalyticProtocol {

}