Guide to Convert Objective-C iOS Apps to Swift

DZone 's Guide to

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.

· Web Dev Zone ·
Free Resource

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.

Figure 1: Create a Swift file
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.

Figure 2: Configure the Bridging Header
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.

Figure 3: Bridging Header helps Project in Communicating with Objective-C and Swift classes

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.

Figure 4: Import the Header Files of Object-C in Bridging Header
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.

Figure 5: Import the Xcode generated background Swift file into Objective-C classes to create Swift objects in Objective-C
Let us summarize this three step process:

  1. When you create your Swift file in an Objective C app, you will need to click accept to create the bridge file.

  2. Put the required Objective-C header files into the bridge header file you created in order to access these classes in Swift.

  3. 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:

  1. Double definitions of variables.

  2. Wrong optional and function signature

  3. Swift version issues

  4. Xcode suggested fixes

  5. 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.

mobile app development, objective-c, refactoring legacy code, swift, web dev

Published at DZone with permission of Salman Saleem . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}