Accessing Objective-C classes within a Swift unit test.

I am attempting to write a unit test in swift that needs to access an Objective-C class. I know that there is a file named "MyProject-Bridging-Header.h" already generated when I added swift files to my project, and a corresponding "MyProject-Swift.h" file. For unit test files, there is also a file called "MyProjectTests-Bridging-Header.h" as well but I do not see a corresponding "MyProjectTests-Swift.h" file. Therefore, I am not able to access Objective-C classes within unit test files that are written in swift. Any suggestions?

Replies

For unit test files, there is also a file called "MyProjectTests-Bridging-Header.h" as well but I do not see a corresponding "MyProjectTests-Swift.h" file. Therefore, I am not able to access Objective-C classes within unit test files that are written in swift. Any suggestions?

Huh? There’s two directions to consider here:

  • Using Objective-C from Swift — To do this you include the relevant headers in your bridging header (

    ***-Bridging-Header.h
    ) and Swift will pick things up from there.
  • Using Swift from Objective-C — To do this, you include the

    ***-Swift.h
    header in your Objective-C code.

So, if you want to do the former you only need

***-Bridging-Header.h
.

It sounds like you’ve added the relevant includes to your app target bridging header (

MyProject-Bridging-Header.h
) but not to your test target bridging header (
MyProjectTests-Swift.h
).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm having a similar problem.

Scenario: Swift UI test of Objective-C project.


This is what I'm getting in the Unit Test file:


The following Objective-C target is in the Pods project:


... so I'm using a Unit Test from within YahooSearchSDK to access YSKBossDataSource in Pods:


Here are the targets:


I created TWO (2) bridge files for the Swift code to access the target Objective-C code:


...still doesn't work.


How do I make my Swift test file 'see' an Objective-C file within the second project, 'Pods'?