27

I have a project with mixed Swift and Objective-C in Xcode 8 that uses the generated "ModuleName-Swift.h" header file to import swift into Objective-c classes, but the preprocessor is not able to find the generated header file and throws an error on the import.

"Lexical or Preprocessor issue : 'ModuleName-Swift.h file not found'"

enter image description here

The project compiles just fine, but the preprocessor throws errors for the header not being found and for any Swift classes called inside the class there is no syntax highlighting or code completion. It's a struggle working with Swift classes in Objective-c that are unrecognized by Xcode 8, but yet compile just fine.

Any ideas on how to appease the preprocessor in Xcode 8?

myuiviews
  • 1,211
  • 1
  • 12
  • 20
  • I'm having the same issue. I noticed in the main project file, the generated header file was named differently than in Xcode 7. So I updated it appropriately and was able to compile. I still haven't figured out how to get the intelisense to work properly. – glycerin Sep 23 '16 at 20:59

5 Answers5

25

I had exactly the same issue. Found the solution after adding a new file aiming to only one target (by mistake) and noticing that it had no problem reading the Swift classes. So, if you have multiple targets, and since the migration you didn't have the need to build and run them, suggest you to do that.

  • 2
    I do have multiple targets in the project and it appears that after building the other target, the preprocessor issues go away. This occurred even without adding any new items without setting the target as indicated, but rather just required building both targets in order for the errors to be resolved. – myuiviews Oct 03 '16 at 18:10
  • @myuiviews that's correct, I meant that I found the fix by mistake, after adding a new file with only one of the targets selected. And this continues to happen, need to build both targets to keep this error away after changes in Swift files. – Ernesto Fernandez Oct 03 '16 at 20:01
  • I get this a lot, and I normally just build and all is good till I edit some file then all imports on the Swift.h files comes back till I build the project again. But most annoying is that new classes are not visible in OBJC and this slows the workflow. – Pascale Beaulac Mar 07 '17 at 21:26
  • @MaudeBeaulac yeah, even when adding/updating a method or parameter, it won't reflect the change in `obj-c` until building the other target.. Hoping it would be fixed at some point. – Ernesto Fernandez Mar 07 '17 at 23:59
  • Saved my day ! – Sjoerd Perfors Apr 06 '17 at 09:20
  • In my case, I had multiple targets, but I had only built one of them. Building all targets removed the error. – Josh at The Nerdery May 03 '17 at 19:42
  • @ErnestoFernandez you are my hero – Ricardo Oct 06 '17 at 12:05
  • @Ricardo I second that! Been searching for this solution for days. – dragonflyesque Feb 20 '18 at 19:31
5

Have this problem when we have multiple targets. If the Objective-c Generated Interface Header Name variable in Swift compiler of newly created targets is different than the original target's value. Change it to the same value with original target. See the following:

change, newtargetname-Swift.h to originaltargetname-Swift.h for new target

enter image description here

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
5

To fix this issue Xcode 9.2, After long research i came to known my "Objective-c Generated Interface Header Name" is named after Product Name "productname-Swift.h"enter image description here

datha
  • 359
  • 2
  • 14
1

You need to add this in your build settings.

There might be an issue while migrating to Xcode 8, where you will have unspecified in the build setting Swift header file.

This if from killerz

Go to Build Settings->Objective-C Generated Interface Header Name and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"

Community
  • 1
  • 1
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
  • I have verified that the Objective-C Generated Interface Header Name is set the same file name that I import in the Objective-C classes. It builds just fine and that file is generated in the derived data, but the preprocessor errors on trying to find it and throws errors for every swift class imported. – myuiviews Sep 26 '16 at 14:00
  • there are no error logs at build time. It's only when editing a file and the preprocessor tries to find the header and import the Swift classes that the error appears in Xcode as described. The actual build works without error. – myuiviews Sep 26 '16 at 14:35
  • @myuiviews Can you share a screenshot of your build settings? – Tal Zion Sep 26 '16 at 15:31
  • 1
    Any feedback on this ? i have exactly the same problem – Mina Zaki Dec 05 '16 at 13:20
1

If

  • your product's normal targets run fine, but
  • you get the error when running your test targets…

note that each target uses (and should use) a different filename for the Objective-C Generated Interface Header Name.

This means you cannot import the generated header file in your Objective-C .h files, because they won't be found in the test target:

screenshot of import error when using .h file

Instead, you should move these #import statements into your Objective-C .m (implementation files), where they'll build successfully.


If you need to refer to Swift classes in a .h file, use the @class directive, e.g.:

//
//  ViewController.h
//  import-example
//

#import <UIKit/UIKit.h>

@class SomeSwiftClass;

@interface ViewController : UIViewController

- (NSString *) titleFromGenerator:(SomeSwiftClass *)generator;

@end
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • "If you need to refer to Swift classes in a .h file, use the @class directive, e.g.:" this was super helpful - Thank you! – Sascha Held Mar 06 '18 at 11:12