Lyle Parkyn
Lyle Parkyn

Reputation: 71

Swift classes inside Objective-C unit test - Incomplete Generated "-Swift.h"

I'm converting an Objective-C app to Swift and using the existing Objective-C unit tests to confirm the changes are working properly.

I replaced a couple of Objective-C app files with Swift and did the following:

Xcode generates the "MyAppTests-Swift.h" file. Examining the generated file (cmd-click), reveals the usual mass of Swift specific #if #defines that precedes where my Swift classes should be located but aren't.

The app will build and run fine with the replacement Swift files. There are other Objective-C files in the app which import "MyApp-Swift.h in order to use my Swift classes." The MyApp-Swift.h" file contains the usual mass of Swift specific #if #defines and my Swift classes from the Swift app files.

Xcode generates "-Swift.h" files for both targets. For some reason, however, the one for the Tests target doesn't receive the Swift class information while the one for the app target does. So it's not an issue of using a feature specific to Swift but not Objective-C.

I've eliminated all compile errors, built from clean but still not working.

Any suggestions?

I'm using XC7 (7C68) iOS 9.1

Upvotes: 4

Views: 998

Answers (1)

Lyle Parkyn
Lyle Parkyn

Reputation: 71

Finally got this working.

I noticed that the Objective-C Bridging Header path for the unit testing target wasn't set while the app target was set. (Swift Compiler - Code Generation build setting) XC will set this path for the app target (probably when it asks if you want to create a bridging header file) It doesn't, however, set the path for unit test target. I guess it assumes unit testing won't need to access the bridging header.

I wasn't concerned about this earlier because the Objective-C Bridging Header is used to import Obj-C code into Swift files. My Swift code wasn't using any Obj-C classes so there's nothing in the bridging header file and so I figured it wouldn't be needed for the unit test target and left the path setting for unit test target at the default blank.

Turns out I was wrong.

On a lark, I set this path to point to the bridging header in the app directory (even though the file doesn't import any obj-c classes). I re-ran the unit tests and now the auto generated "MyAppTests-Swift.h" file contains the Swift classes that are referenced by the obj-c unit tests.

Seems a bit quirky. Xcode needs a path to an unused file (bridging header) in order to cause it to add referenced swift classes in the auto generated "-Swift.h" file.

Upvotes: 3

Related Questions