Guide to Convert Objective-C iOS Apps to Swift
Several years ago Apple switched its main dev language from Objective-C to Swift. If your now looking to follow suit, read on to learn how.
Join the DZone community and get the full member experience.Join For Free
Apple launched its new native language called "Swift" back in June 2014 for creating apps for iPhone, iPod, Apple Watch and other apple devices. Apple promised that it is a lot faster and simpler than Objective-C. In this article, we will discuss how to convert a legacy iOS app written in Objective-C language to Swift without rewriting all the code from scratch.
To understand this article, you should be familiar with both Swift and Objective-C. Here is a quick getting started to Swift article. Another guide for Swift (https://developer.apple.com/swift/) is there to help you get going.
Step 1: Create a Swift File
The first step is easy and as simple as creating a Swift file in the Objective-C app. To create a Swift file, go to New File menu in Xcode and then select iOS source and then pick Swift file from the iOS Source menu. Hit the Next button. Write the name of the file and click the Create button.
Now you will see the bridging header pop up in Xcode (see Figure 2). Xcode automatically creates a file called "bridging header" and another file in the background which we will talk about in a minute.
The bridging header file allows your project to communicate with both Objective-C classes and Swift classes. So, the project is configured to work in mixed mode where the code can live as an Objective-C file or as a Swift file.
Step 2: Import All Header Files Into a Bridge File
After the bridge header file is setup, we will move all of our project header files from the Objective C classes that we want to use in Swift into this bridge-header file.
This bridge header file allows us to create Objective-C objects in Swift classes. At this stage, we can create objects of Objective-C classes in Swift, however, if we try to create Swift classes objects in Objective-C classes it will show an error. Why?
Step 3: Import Background Swift FileGenerated in Xcode to Allow Swift Objects in Objective-C
The bridge file allows Objective-C classes to be imported into Swift classes but does not allow Swift classes/objects to be created in Objective-C classes. To solve this issue, we will need the background file generated by Xcode when we created the Swift file in Step 1. We will need to import that hidden (Xcode generated background file) Swift file into Objective-C classes to create Swift objects in Objective-C. The name of that file will use the following naming pattern: "NameOfTheProject-swift.h". In the example, my project name is "Guilder," so the Xcode generated file name is "Guilder-swift.h". Look at the screenshot given below.
Let us summarize this three step process:
When you create your Swift file in an Objective C app, you will need to click accept to create the bridge file.
Put the required Objective-C header files into the bridge header file you created in order to access these classes in Swift.
In your Swift classes, where you want to access Objective-C classes, you will import the hidden Xcode generated file.
Sometimes the project is huge in terms of source code and it takes a lot of time to change each individual file written in Objective-C to Swift, making it seem easier to rewrite the whole project from scratch. In that situation, you can use online and desktop converter tools to convert you Objective-C Swift. One such tool is Swiftify. Once you convert your application using these tools, you may still see some bugs and syntax errors like:
Double definitions of variables.
Wrong optional and function signature
Swift version issues
Xcode suggested fixes
- Null checks
However, these issues can be addressed, and these tools save a lot of time and labor. For instance, to solve the double definition of variables issue, you will need to remove duplicate declarations. Another exampls is how you can resolve the optionality issue, by using if..else or let guard statements, instead of unwrapping the variables. This is because unwrapping is prone to errors.
To conclude, the bridge header mechanism along with hidden Swift files generated by XCode provides a way for Objective-C and Swift classes to talk to each other and consume each other's services. So, we can create wrapper classes in Swift and consume Objective-C classes to provide the same functionality without rewriting anything.
Published at DZone with permission of Salman Saleem. See the original article here.
Opinions expressed by DZone contributors are their own.