Codementor Events

Swift & Objective-C: How to Use 2 Languages in Your Project

Published Jan 03, 2017Last updated Jan 18, 2017
Swift & Objective-C: How to Use 2 Languages in Your Project

In 2014, Apple surprised the entire developer community by announcing Swift, an all new programming language. Two years later, it has been widely adopted by programmers around the world.

If you haven’t yet had a chance to play with Swift, it’s a great time to get started. Swift has undergone a number of important revisions and improvements since its initial release and is now generally considered stable enough for full-time use.

Choosing between Swift and Objective-C could be confusing for beginners. And similarly, many of you are likely still using Objective-C in your older iOS applications. Converting your existing apps from Objective-C to Swift doesn’t make a whole lot of sense but using Swift going forward is a smart way to future proof your app while learning a new language at the same time.

But today, we will be discussing how you can use Swift and Objective-C in your projects together. It’s not always easy, or as straightforward as Apple promises, but with a small amount of work you should be able to have both languages co-exist in a single project.

Using Swift in your Objective-C project

If you created your project more than two years ago, it will have been written in Objective-C. Luckily, Apple has created a way to use both languages inside your project. This makes it easy to start preparing for an all-Swift future that is coming quicker than many developers imagined.

The great thing about using a mixed-language project is that you can decide how far you would like to go. You can use a few Swift files to for certain classes and Objective-C for others.

One thing is for certain, using Swift inside your existing Objective-C project is a great way to learn about the new language without having to fully commit to it.

Creating a new Swift file

To get started using Swift, use the File > New > File… menu to create a new file. Choose the type of file you would like to create and press next. You can choose the name of your class, what it is a subclass of and the preferred language of the file.

swift objective-c

We will be creating a UIViewController subclass called ViewControllerSwift. Choose the Swift language and press the next button to continue on to the next step. Create the file and be sure that it is listed in the Project Navigator, on the left side of the Xcode application (where your project files are located).

Automatically adding a bridging header

If this is the first time you’re adding a Swift file to your project, you will be asked whether you want to create a bridging header. A bridging header allows you to use both languages inside the same app. Choose Create Bridging Header to continue.

swift objective-c

Manually adding a bridging header

If you previously chose Don’t Create, you won’t be prompted again to add a bridging header. In some cases, this has caused a bit of confusion among developers trying to get mixed-language projects to work. If Xcode didn’t prompt you to create a bridging header, you can create it manually instead.

To add one manually, add a new file to your Xcode project (File > New > File...) then select “Header File” and click the next button. Name your file “YourProjectName-Bridging-Header.h”.

Be sure to type your own actual project name instead and take note of the capitalization of the words. If your project name includes a space (Example: “Hello World”) use an underscore between the words.

Accessing the Swift file from Objective-C

Now that our new Swift file is created and our bridging header is in place and sitting comfortably in our project navigator, let’s try to access the Swift View Controller from one of our Objective-C classes.

Inside our ViewController.m (Objective-C) file, type “View” to force the autocompletion feature to show us our options. You’ll notice that it gives a few similarly named files but it doesn’t include our newly created Swift file.

swift objective-c

It seems Xcode needs a bit more direction from us.

We need to add an #import statement to the Objective-C file to allow it to see the Swift files inside your project. The header file that we are importing was automatically generated in the background for you when you created your first Swift file and therefore needs only to be added to your file.

Note: This header file is different than the bridging header we added earlier. This header file does not show in your project navigator, it’s all behind the scenes.

The name of this auto-generated file is “ProductModuleName-Swift.h”, where ProductModuleName is your project name. In our case, it’s simply “ObjectiveProject-Swift.h”.

Double checking the header name

Using two languages in one project can be troublesome, so let’s double check the header name that Xcode created.

Click your project at the top of the Project Navigator pane (left side of Xcode), then click your Target. Click Build Settings and type in “Swift” to the search box to narrow down the results.

swift objective-c

Look under the "Objective-C Generated Interface Header Name" key to view the name of your header file. In our case, it’s exactly as we thought it would be.

Importing the header

Now that we have our header name, let’s add it to the Objective-c.m file where we want to access the Swift file. Add an additional #import statement to the top of your file with the name of your header.

swift objective-c

Accessing the Swift file

With our import statement in place, your Objective-C file should be able see the Swift file. We say should because, well, it’s Xcode and sometimes things don’t work the way you think they should.

Typing “View” again pops up the autocomplete feature and this time you can see that our Swift file named ViewControllerSwift is now one of the listed options.

swift objective-c

Wrapping up

That’s it for today. As you have learned, using two languages in your iOS, or Mac app is pretty straightforward and not too difficult to implement. Adding Swift functionality to your project is a great way to get familiarized with the new programming language while keeping your existing Objective-C files as they were.


Author's Bio

swift objective-cRyan Hartman is a senior iOS developer with over 7 years of experience creating awesome apps for iOS and Mac. Located in beautiful Berlin, Germany, he is passionate about helping others learn about programming, UI/UX design, and other technical topics.

Discover and read more posts from Codementor Team
get started
post commentsBe the first to share your opinion
Anna Zubarieva
7 years ago

Thanks for the useful article! Swift is really spread among developers. Also, I recommend to read this comparison between Swift and ObjectiveC: erminesoft.com/objectivec-vs-swift/

Alexey Porubay
7 years ago

Hello everyone!
I have nice article about it here: https://www.cleveroad.com/blog/swift-vs-objective-c--what-is-the-best-language-for-ios-development-
You need to understand, that you can use languages for different features.

Dhiraj Kumar
7 years ago

HI ,
i have some query regarding your tutorials
(link : https://www.codementor.io/codementorteam/swift-objective-c-using-2-languages-in-your-project-ucm16ki0l )

Problem:

i dropped swift file into my project and created bridging header , it takes to much time to compile and when i try to Archive it take around 30-45 min , can you please tell me how can i disable to compile Swift Library i just want my project in Objective-c .
i deleted Swift file but not able to disable Swift compilation .

can you please help me out ?

Thanks in advance !

Dhiraj kumar

Show more replies