Forrest
Forrest

Reputation: 127643

Unit testing with swift 2 for Objective-C project

I want to build the unit testing in xcode 7 with swift 2 for old objective-c project.

The pro is 1/ try to use swift with new features 2/ use new xc test framework from xcode 7

Any good practice for this way ? Thanks for sharing

Upvotes: 3

Views: 767

Answers (1)

rony_y
rony_y

Reputation: 605

In Xcode 7 create a new Swift file (File -> new ->SomeClassName.swift) and then Xcode will prompt you to add a bridging header press yes.

In the bridging header file you import the classes you want to expose to swift #import yourSomeClass.h".

Just a side note if you want to use your .swift class in a objective c class you need to add this import statement to your .m file :

import yourProjectName-Swift.h>

Now create a unit Testing target file -> new -> target select the Test tab and create a new unit testing target in the Swift language.

Go to "build settings" in your project navigator in the search box type bridging and make sure its the same header path as the project target uses and in the Swift compiler - code generation make sure the "install objective c compatibility header is set to YES".

don't forget to import your class to the Bridging-Header.h file.

Basically thats it in the .Swift test file declare var / let someInstance = YourClass () and do the relevant testing.

Enjoy.

Upvotes: 5

Related Questions