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… 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.
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.
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...) 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.
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.
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.
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.
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.
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.
Ryan 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.