Import Swift protocol into Objective-C class

I am trying to import a Swift protocol named AnalyticProtocol into an Objective-C class named AnalyticFactory .

 protocol AnalyticProtocol { } 

I start with an existing Objective-C project (I did not create a new Swift project with xCode, and I did not find how to configure my Objective-C project as a Swift project in xCode 6 ).

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

In the Apple documentation , they said that I should include my .h file named MyProjectName-Swift.h in my .m file. But I need to include it not in my .m file, but in my .h . Could this be problematic?

When I try to compile, I have this error:: 0: error: xxxAnalyticFactory.h: 39: cannot find the 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 how I can import the Swift protocol into the Objective-C class.

Does anyone see a mistake in what I'm doing?

+101
objective-c swift
Jun 06 '14 at 9:15
source share
4 answers

You need to add the @objc attribute to your Swift protocol as follows:

 @objc protocol AnalyticProtocol { } 
+192
Jun 06 '14 at 12:14
source share

It is not possible to import the Swift header generated by Xcode into objC header files.

So, since you want to use the Swift code in the objC header file, you will need to “forward the declaration” to the classes and protocols that you want to use in the objC header file, for example:

 @protocol AnalyticProtocol; 

Now you can use the protocol in the declaration of the objC class:

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

In your implementation file (objC.m file) you can import the Swift-generated Xcode header ("ProductModuleName-Swift.h"), and the correct implementation of the AnalyticProtocol will now be known to the compiler.

This is also described in "Using Swift from Objective-C" in Documents for Apple

Note that Xcode will issue a warning in the objC header file when you use the protocol declared ahead ("Cannot find the protocol definition for" AnalyticProtocol "), but this can be ignored - the implementation will be found at compile time.

+68
Feb 04 '15 at 5:35
source share

For those who just need to accept the protocol, you can do this in two steps without generating any warnings or errors:

  1. In the .swift file .swift add @objc in front of the protocol name:

     @objc protocol AnalyticProtocol { } 
  2. In your .m file, import the generated Swift header and accept the protocol in the private category. (The header file is automatically named):

     #import "ProductModuleName-Swift.h" @interface AnalyticFactory () <AnalyticProtocol> @end 

This is the apple recommended approach. You can learn more about mixing and matching Objective-C and Swift here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

+37
Dec 23 '14 at 19:06
source share

If you are creating a framework, import is required

 #import "ProductModuleName-Swift.h" 

changes in:

 #import <ProductModuleName/ProductModuleName-Swift.h> 

In this case, you should also make a public protocol:

 @objc public protocol AnalyticProtocol { func something(); } 
0
May 21 '19 at 16:11
source share



All Articles