If you have an Obj-C project that’s grown some Swift, you might find yourself with some Obj-C test code that needs to talk to some Swift code.
You’re likely to run into just one little problem. You can’t import the header that actually declares the Obj-C interfaces to all that Swift code.
More precisely, the compiler can’t find the header, even though you know it’s there, because Obj-C code in your main app is using it left and right.
You’re looking at an error like this:
In file included from /Users/jeremy/Downloads/MixedLanguageTesting/MixedLanguageTestingTests/ObjCTests.m:10: /Users/jeremy/Downloads/MixedLanguageTesting/MixedLanguageTesting/ObjCClassUsingSwiftClass.h:15:9: fatal error: 'MixedLanguageTesting-Swift.h' file not found #import "MixedLanguageTesting-Swift.h" ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
No good, that. Luckily, the Usual Fix works.
The Usual Fix: Fix your header search path!
A file is there. A compiler cannot find the file. You need to tell the compiler where to look to find the file. That is what search paths do.
In particular, you probably want
USER_HEADER_SEARCH_PATH here, which tells
the compiler what paths to hunt through when searching for user, rather than
system, headers. (Those would be the ones you import like
<system.h>, hence the actual build flag’s name,
That path: The app target’s derived sources directory
You need your test target to have a look through the app target’s derived sources directory.
To do that, edit your project settings. (Or each and every test target’s
settings. But that just sounds painful.) Search for the
setting, open up the setting, and add this line:
Only, rewrite that “MixedLanguageTesting” bit to match your app target’s name.
Behind the Scenes
If you want to see how I arrived at this solution, I happen to have checkpointed my debugging steps using git. You’ll find my trail over at GitHub at jeremy-w/MixedLanguageTesting.