How to access swift code in main target from objective-c code from test target?

522 Views Asked by At

I have swift class descendant of NSObject and marked with @objc. I done everything described in Apple doc about interoperability and numerous question on stack overflow. My Xcode-generated header (TargetName-Swift.h) is created by Xcode (I know it for sure - it is in derived data folder, and I can access swift code within the same target from objc by importing TargetName-Swift.h). However, I can't access Xcode-generated header from my unit tests target. Xcode complains that #import <ProductName/ProductModuleName-Swift.h> file not found. I've checked product/module name multiple times - they are correct.

It won't be found even in new project created with Xcode 7.3. (despite settings Defines module to YES, declaring swift classes public, @objc and descendant of NSObject/ViewController/etc)

What am I doing wrong? Could it be a bug in Xcode?

1

There are 1 best solutions below

0
On

It seems like the only way to access swift code from objective-c code in different target is to add Xcode-generated header to User Header Search Path in target where objective-c code located.

In case your swift code is in main target and objective-c in tests target you could use $(CONFIGURATION_TEMP_DIR)/$(PROJECT_NAME).build/DerivedSources. Note that this path assumes that your main target has the same name as Project name (in most cases its true since Xcode creates such names by default).