12

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?

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • Why are you importing project-swift.h in your Objective-C file? Are you trying to call a swift function in Objective C file ? If not there is no need to call project-swift.h in your Objective-C file – Sandeep Bhandari Dec 06 '17 at 17:12
  • I'm trying to access my KeyPair class which is a swift class defined as `@objc final class KeyPair` with an `@objc init` – Zack Shapiro Dec 06 '17 at 17:13
  • Possible duplicate of [Importing Project-Swift.h into a Objective-C class...file not found](https://stackoverflow.com/questions/26328034/importing-project-swift-h-into-a-objective-c-class-file-not-found) – Jakehao Feb 16 '19 at 05:54

5 Answers5

26

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
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
4

I had similar issue and almost ended up spending a whole day trying to figure out what wrong with my app. So following the solution that's helped me :

  1. Clear derived data
  2. Create a class in swift with prefix of @objc for example @objc class mySwiftClass{...}
  3. Build the project again

Et voila.. Should work now.

Why to add @objc? this @objc prefix, tells the compiler to generate to your swift class a header file. it will add it to the "MyModule-Swift.h" file

1

I realize this is an old thread, but I had similar issues after adding a new target to a project. I solved it by adding a preprocessor macro (Build Settings -> Apple Clang - Preprocessing) only in said target and then importing the Swift.h file conditionally, like this:

#if DEV_VERSION
#import "Project_DEV-Swift.h"
#else
#import "Project-Swift.h"
#endif

My main target is called Project and the new target is Project DEV (the space is replaced with an underscore in the import), and the preprocessor macro is called DEV_VERSION.

After doing this, both targets build just fine.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Xcode compile time error

<product_name>-Swift.h file not found

It is a kind of bridge(adapter) between Swift and Objective-C. This file contains Swift's API for Objective-C which was marked [@objc and @objcMembers].

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file.

The header's name is generated from a <product_name>-Swift.h

[Mixing Objective-C and Swift ]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

I had a similar issue whereby it would have this issue for anything other than live. I resolved the issue by hardcoding "Product module name" & "Product name" to my project name. This avoids the need to have preprocessor logic in every file that includes swift code as demonstrated in Pauli Kettunen's solution.

Mark
  • 1