7

I'm trying to call a method of a NSObject subclass I have in the let's say "main" project within a method of a XCTestCase subclass in the test project. The NSObject subclass of the main project imports the "XXX-Swift.h" header file (I'm mixing Swift and Objective-C code). All works fine when I run the main project, but when I run the tests calling an object of that subclass, I get an error saying that the Swift header file in the import is not found.

I guess I'm missing some settings in the test target, but I don`t find which... what should I check? Or what should I do to solve this?

AppsDev
  • 12,319
  • 23
  • 93
  • 186

1 Answers1

16

Go to Xcode Project > Build Settings and type "header name", you will get the below option:

enter image description here

Make sure that there must exist a file with a name: $(SWIFT_MODULE_NAME)-Swift.h

Then type, "bridging header" under Build Settings and make sure that the path of "bridging-header.h" file exist correctly.

enter image description here

Edited:

1) Make sure that your "Project Name" doesn't contains spaces 2) Well "XXX-Swift.h" file is generated at the following path, so you need to add that path to unit test - Go to build settings, type "Header Search Paths" and add the following line

"$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"

For more, you can refer this link : iOS - 'MyProject-Swift.h' file not found when running Unit Tests for Swift

Community
  • 1
  • 1
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • I already have those settings in the app target, and I've set `Embedded Content Contains Swift Code` to YES in the test target, but it is still working when I run the app, but gettings the error when running the tests... – AppsDev Jul 22 '15 at 08:22
  • I still get the error... how could I see the value of $(TARGET_TEMP_DIR)? – AppsDev Jul 22 '15 at 08:44
  • @AppsDev You can know it from here: http://stackoverflow.com/questions/4233581/xcode-4-build-output-directory – Sohil R. Memon Jul 22 '15 at 08:55
  • After doing this my test target can see XXXTests-Swift.h however the file contains a line to @import XXX; which it cannot resolve – cynistersix Apr 27 '17 at 16:18
  • Remember to do the import of 'MyProject-Swift.h' in the .m file AND ALSO VERY IMPORTANT IN MY CASE in a place not limited by #ifdef FB_SONARKIT_ENABLED or #if DEBUG! – scbojer Mar 04 '21 at 10:03