I have a new Swift project with a few files, I've needed to add some Objc code.
In Build Settings
, my Objective-C Generated Interface Header Name
is MyProject-Swift.h
Product Module Name
and Product Name
are both MyProject.
My Objective-C Bridging Header
is MyProject/MyProject-Bridging-Header.h
The contents of my Bridging Header
are:
#ifndef MyProject_Bridging_Header_h
#define MyProject_Bridging_Header_h
#import "Blakey.h"
#endif
Blakey.h
is pretty simple:
@import Foundation;
#import "MyProject-Swift.h"
@class KeyPair;
@interface Blakey: NSObject
- (void)createKeyPairForSeed:(NSString *)seed;
@end
And Blakey.m
#import <Foundation/Foundation.h>
#import "Blakey.h"
@implementation Blakey
- (void)createKeyPairForSeed:(NSString *)seed;
{
}
@end
(side note: I'm aware my function returns a void, that will be changed later once this issue is fixed so it returns an actual value)
Why is Xcode throwing an error at the #import "MyProject-Swift.h"
in Blakey.h
?
${Your project name}-Swift. h is for objc based project to use swift code I remember. It is generated by XCode, to translate swift code to a header file for objc to import. If you don't need to use swift in objc project, you don't need it.
Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .
Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File. Edit the bridging header to expose your Objective-C code to your Swift code: In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.
Project-Swift.h
is a file auto generated by Xcode on successful compilation of the project. Catch here is the word successful compilation If your project has any compilation error Project-Swift.h
file will not be generated. So in a way it becomes a deadlock. Bestway comment out all the lines that have compilation error and then manage to get it compile without any errors. Only after that Project-Swift.h
will be generated.
Additional information, Once the Project-Swift.h
file is generated if you open it and if you happened to see that your swift class is not imported there thats because Project-Swift.h
imports only the classes that extends from NSObject So plain Swift classes will not be imported.
ISSUE:
You need to import Project-Swift.h
in .m file and not .h file. So modify your Blakey as
#import <Foundation/Foundation.h>
#import "Blakey.h"
#import "MyProject-Swift.h"
@implementation Blakey
- (void)createKeyPairForSeed:(NSString *)seed;
{
}
Finally remove #import "MyProject-Swift.h"
from Blakey.h
@import Foundation;
@class KeyPair;
@interface Blakey: NSObject
- (void)createKeyPairForSeed:(NSString *)seed;
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With