Kyle Decot
Kyle Decot

Reputation: 20835

Xcode cannot find ProductModuleName-Swift.h

I'm attempting to import my "-Swift.h" file into one of my Objective-C .h files but xcode keeps telling me that the file doesn't exist

#import "Aesculus-Swift.h"

If I command click on the file name it will take me to the generated header file so I know it exists. Why is xcode not able to find it?

enter image description here

enter image description here

Upvotes: 5

Views: 2470

Answers (4)

user2595925
user2595925

Reputation: 108

When you encounter such situation, just find your kinda "ProductName-Swift.h" file by just cmnd+click on it (even if xcode shows warning about it is not found, the #import "Aesculus-Swift.h" string is still clickable) and then in opened code editor window choose context menu and "Show in Finder" item, then explicitly add it to your project.

enter image description here

Upvotes: 1

dijipiji
dijipiji

Reputation: 3099

I had trouble with this stuff & found that your -Swift file is the Product name of your Target ( not just the name of your Target ) . I found the details here helpful: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/

Upvotes: 2

Ewan Mellor
Ewan Mellor

Reputation: 6857

I solved this recently by adding the following entry to my .xcconfig (you could add it in Xcode's Build Settings > User Header Search Paths if you prefer).

USER_HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)/MyFramework.framework/Headers

This tells the compiler to search for headers in the build output directory, which is where Xcode puts the generated header (at least in the case of this framework).

In my case this is a directory like ~/Library/Developer/Xcode/DerivedData/MyProject-LongCode/Build/Products/Debug-iphonesimulator/MyFramework.framework/Headers/MyFramework. You might find your generated header in there too.

Xcode's header and dependency management is a hot mess, and it's not surprising that it doesn't work for you.

Upvotes: 2

Jessedc
Jessedc

Reputation: 12460

This seems like just another issue with Xcode and it's complex tool chain of static analysers and compilers.

Openradar lists radar://21362856 - Swift to Objective-C bridging is unreliable. I am sure there are more but I stopped looking after finding one for this example.

The author imarcelv notes in the description:

I asked a Swift engineer at WWDC in a lab and even he didn't know how to fix this issue.

Steps to Reproduce:

  1. Add a ramdom Swift class to an Objective-C project
  2. Add the #import "ModuleName-Swift.h" file that Xcode generates automatically
  3. Try to use it or just try to compile the project
  4. From time to time it simply doesn't work

It's probably best to file a radar on this issue as it seems that others are already calling it out.

One other thing you could try...

Historically, it was possible for Xcode to completely lose it's syntax highlighting and you could always find out what files the static analyser was giving up on by increasing log level of clang.

I'm not sure if it's still relevant but if I was in your position I'd be trying this command:

defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3

This generates logs you can search with using Console.app for just xcode to highlight the messages. You'll want to trash the derived data of your project to force it to re-compile things.

Although not the same issue as what you're seeing, I have had this post on the syntax highlighting issue bookmarked for years for the above defaults write command to try in times like these.

Upvotes: 5

Related Questions