13

I have followed this recipe in order to use a Swift class in an existing Objective-C project MyProject. That works fine.

However, I'm still not able to use the same Swift class in the same project's unit tests. The compiler marks the line where my Objective-C unit test says #import "MyProjectTests-Swift.h" with file not found.

If tried changing the test target's Product Module Name from its default MyProjectTests to MyProject as suggested in a comment to this (unanswered) question. However the compiler now marks the line where my Objective-C unit test says #import "MyProject-Swift.h" with file not found.

So how can one integrate Swift classes into (XCTest) unit tests that are written in Objective-C? Does Apple provide any recipe?

Community
  • 1
  • 1
Drux
  • 10,334
  • 10
  • 58
  • 107
  • Duplicate of http://stackoverflow.com/questions/24932147/how-should-a-swift-objective-c-project-be-setup-for-unit-testing ? – Martin R Jan 23 '15 at 17:01

3 Answers3

16

As per @SushiGrassJacob's comment (see here) the following solves the issue:

  1. Make sure that Swift classes are targeted to both MyProject and MyProjectTest.
  2. In unit test, #import "MyProjectTests-Swift.h".
Community
  • 1
  • 1
Drux
  • 10,334
  • 10
  • 58
  • 107
  • 3
    Thx! this was finally the answer I was searching for. Its really stupid to have to add Swift files to test target -.- – Buju Dec 17 '15 at 14:42
  • 1
    Adding files to the test target only works with a simple project with minimal imports. If your swift class inherits from an objc class then it will fail. – Jason Moore Jun 11 '19 at 14:28
7

Step-1

Go to your test target > Build Settings > Header Search Paths, and add $CONFIGURATION_TEMP_DIR/YourProject.build/DerivedSources in it.

Step-2

#import "YourProject-Swift.h"

Harry Zhang
  • 589
  • 6
  • 5
0

Go to test target > Build Settings > Header Search Paths, and add $CONFIGURATION_TEMP_DIR/MyProject.build/DerivedSources in it. For me worked only without checking Swift classes targeted for MyProjectTests. Source: https://medium.com/if-let-swift-programming/ios-tests-working-with-objective-c-and-swift-class-together-aaf40f91a27c

nemissm
  • 383
  • 4
  • 9