Tung Fam
Tung Fam

Reputation: 8167

myProjectName-Swift.h not found after clean build

Spent a lot of time on finding the solution on google and SO but no success. Very hope someone can point out what can be the problem.

So I have objc+swift project. I have a Bridging Header file with imports of Objc header files that I need to use in Swift.

To explain the issue I'll share 2 scenarios. In the 1st scenario, everything works fine. In 2nd it shows an error.

Please note: Both scenarios have the same code base.

Scenario 1. Doesn't work, shows error.

  1. I open the project.
  2. Hard Clean it.
  3. Build
  4. Shows error: 'myProjectName-Swift.h' file not found
  5. It also shows such error:

failed to emit precompiled header '/Users/tungfam/Library/Developer/Xcode/DerivedData/myProjectName-ctxxkwqtckhvyoawavmuzmdxqaml/Build/Intermediates.noindex/PrecompiledHeaders/myProjectName-Bridging-Header-swift_1UP5PCPCLBPHP-clang_18PVO5108TD8S.pch' for bridging header '/Users/tungfam/Developer/myProjectName/myProjectName/App/myProjectName-Bridging-Header.h'

Scenario 2. How to make it work.

  1. I take the same code from Scenario 1.
  2. Hard Clean it
  3. Comment the imports in Bridging Header file
  4. Comment the Swift code where I used Obc files (that were declared in bridging header that I just commented in previous step)
  5. I build the project. It succeeds.
  6. Since some parts in the app are broken cuz I commented stuff. I uncomment the imports in Bridging Header file and the Swift code where I used Objc classes.
  7. Run again (without hard clean) and everything works.

Will really appreciate any help on this issue. Please share anything you think that may help to fix this issue.

I'm using Xcode 10.0; Swift 4.2

UPDATE1: I think it has to do something with the 2nd error I placed above. Maybe it can't generate that bridging file.

UPDATE2: I read something like: "If you are importing the Objc file named ABC into Bridging Header. And if this ABC imports into himself the file myProjectName-Swift.h. Then this case may have some problems. Do you think it can be true?

Upvotes: 0

Views: 2709

Answers (2)

dosi
dosi

Reputation: 476

You must not use #import "ProjectName-Swift.h" in the header files.

If you need Swift classes or protocols in the Obj-C code, you may forward declare them in the related Obj-C header. Here's more information about that:

When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.

// MyObjcClass.h
@class MySwiftClass;
@protocol MySwiftProtocol;

@interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
@end

Also, please note, you may have issues with importing Swift Enums and Protocols and Classes into ObjC, so you may need to explicitly define items which you want to be available to ObjC code with @objc keyword.

And you won't be able to use Swift structs in Obj-C.

Upvotes: 1

Ramon Vasconcelos
Ramon Vasconcelos

Reputation: 1466

From Swift to Objective C you just have to use #import "ProjectName-Swift.h" on your Objective C classes that needs access to Swift code. There's no need to add on the bridging header file. For the other way Objective C to Swift then you need to declare in the bridging header file.

Upvotes: 0

Related Questions