Swift framework - Use Swift class reference in Objective-C class

2.4k views Asked by At

I am creating Swift framework in which I have to use Objective-C class. So I went through this link. This is the public header of my framework :

#import <UIKit/UIKit.h>

//! Project version number for Test.
FOUNDATION_EXPORT double TestVersionNumber;

//! Project version string for Test.
FOUNDATION_EXPORT const unsigned char TestVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <Test/PublicHeader.h>

#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>
#import <MyTest/MPAppDelegateProxy.h>  

Now in class MPAppDelegateProxy, I have to use a Swift class which I have created. This is :

#import "MPAppDelegateProxy.h"
#import "MyTest.h"

@implementation MPAppDelegateProxy

+ (void)proxyAppDelegate {
    [MPGlobal MPLog:@"App delegate not set, unable to perform automatic setup." file:@"MPAppDelegateProxy.m" function:@"proxyAppDelegate" line:32];
// rest of code
}  

MPGlobal is one of my Swift class. But I am getting :

Use of undeclared identifier 'MPGlobal'

Note : I have added @objC before MPGlobal.

2

There are 2 answers

4
Puneet Sharma On

You need to import <Target>-Swift.h file. This is known as Objective-C Generated Interface Header Name.

You can find it in your Target's build settings.

enter image description here

This file is auto generated by compiler and it needs to be imported in Objective-C files.

3
Mohmmad S On

change the SWIFT_OBJC_INTERFACE_HEADER_NAME build setting and making it the same across different targets. To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.h to $(PROJECT_NAME)-Swift.h as explained here

After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.

If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:

SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
@interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

If need to ensure that those headers are being generated open a terminal and use this command

find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"

You should see one header for each target

Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:

Exactly where at the top of a .m file you #import the hidden bridging header can make a difference. The usual sign of trouble is that you get an “Unknown type name” compile error, where the unknown type is a class declared in Objective-C. The solution is to #import the .h file containing the declaration for the unknown type in your Objective-C files as well, before you #import the hidden bridging header. Having to do this can be an annoyance, especially if the Objective-C file in question has no need to know about this class, but it resolves the issue and allows compilation to proceed.

At the very end the code compiles and runs on device and simulator!

Original answer

Also you can try this, You needed to import the -Swift.h for for both the framework and the app target

For Example :

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "XLPagerTabStrip-Swift.h"
#import "RealmSwift-Swift.h"
...... // Add all frameworks, subclasses, and dependance ios frameworks
#import  "MyProject-Swift.h"

You can read this article How to import file header and check paths