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 into modern swift language without rewriting the whole code from scratch.
So let's dive in the details and check a step by step guide with elaborating example on how can we port an Objective C app into Swift iOS app? It is becoming handy in making your mobile applications aligned with the latest Custom Mobile Application Development trends around the world.
To understand this article, you should be familiar with both Swift and Objective C languages. If you are not familiar with any of these languages, it will be hard for you to understand this article. Here is a quick get 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 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 on the Xcode (See the 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 Objective C file or as a Swift file.
Step 2: Import all header files into bridge file
After the bridge header file is setup, we will move all of our project header files of 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 Xcode generated background Swift file to allow Swift objects in Objective-C
The bridge file allows Objective C classes to be imported swift classes but does not allow Swift class objects to be created in Objective C classes. To solve this issue, we will need the background file generated by Xcode when 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 be like “NameOfTheProject-swift.h” according to your project name. In the example I build, my project name is “Guilder” So the Xcode generated file name is “Guilder-swift.h”. Look at the Figure 5 given below.
Let us summarize this three-step process:
1. When you create your swift file in an Objective C app, you will need to accept click on create bridge file.
2. Put the required Objective C header files in the created bridge header file to access these classes in Swift.
3. In your swift classes where you want to access Objective C classes, so 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 that it seems easier to rewrite the whole project from scratch. In that situation, you can use online and desktop converter tools to convert you Objective C app to a Swift app. One such tool is Swiftify. Once you convert your application using these tools, you may still see some bugs and syntax errors like
1. Double definitions of variables.
2. Wrong optional and function signature
3. Swift version issues
4. Xcode suggested fixes
5. Null checks etc.
However, these issues can be addressed, and these tools save a lot of time and labour. For instance, to solve the double definition of variable issue, you will need to remove duplicate declarations. 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 file generated by XCode provides a way for Objective C and Swift classed to talk to each other and consume each other services. So, we can create wrapper classes in Swift and consume Objective C classes to provide the same functionality without rewriting anything.
Muhammad Bilal is lead iOS App Developer at Techliance, a leading Custom Information Technology Services Company in United States. He is passionate about Apple platform and is keen to learn new technologies introduced by Apple.
This post was originally published as How to Convert an Objective-C IOS App to Swift on the Techliance Blog.