How to Migrate Your Objective-C Project to Swift?

You are the developer of an iOS app written in Objective-C. However, you will undoubtedly be aware of the growing popularity of Swift.

Naturally, in order to stay ahead in the marketplace, you will be wondering how to migrate your Objective-C project to Swift.

Look no further! I will explain how to do this in this article.

Contents

What is Objective-C?
Enter Swift!
The growing popularity of Swift
Advantages of Swift
How to work with Swift?
How can you convert your project from Objective-c to Swift?
Planning a bigger Objective-C to Swift conversion project?

What is Objective-C?

Developed in the late 1980s, Objective-C was modeled on the path-breaking C language. Brad Cox created it as an extension of the famous C language. Steve Jobs’ NeXT Software licensed Objective-C in 1988. Read more about this early history in “Learn Objective-C: a brief history”.

Eventually, Jobs returned to Apple. Apple, under his guidance, made heavy use of Objective-C. The language became a mainstay for OSX and iOS, which are Apple‘s desktop and mobile operating systems, respectively.

Objective-C has created a small number of extensions to the ANSI C programming language. These are powerful extensions, therefore, they enabled advanced object-oriented programming. Most Apple frameworks are written in Cocoa programming, which in turn is built on Objective-C.

Enter Swift!

We all know about Apple and their loyal users! Apples‘ popularity, resulting from the sophistication of their products, shows how powerful Objective-C really is. Accordingly, the language has always enjoyed a high degree of popularity.

It started to change in 2014 when Apple released their Swift language. Creators of Swift used open-source frameworks and took ideas from Objective-C, Python, Ruby, C#, and a few more languages. There have been multiples releases of Swift, with the last one being launched in October 2018.

Swift is a general-purpose programming language. Developers increasingly use it to develop apps for iOS, macOS, watchOS, and tvOS. It works well with Apples‘ Cocoa and Cocoa Touch frameworks.

The growing popularity of Swift

Although the first release of Swift was only in 2014, the language rapidly gained popularity. An InfoWorld report published even as early as 2015 highlighted its‘ popularity. The report also showed the consequent decline in the popularity of Objective-C. Read the report in “Objective-C plummets in popularity”.

Download Our Project Specification Template

The trend continued in the subsequent years. It‘s not clearly known how many apps use Swift. However, this doesn‘t have much importance. As Antonio Nesic, an open-source expert points out, Swift is built on the Cocoa framework.

Antonio points out that this framework itself is built on Objective-C, therefore, developers can easily mix Swift code with Objective-C code. The question of how many apps use Swift isn‘t relevant. Read more about his interesting insight on this, in this Quora Q&A thread from 2017.

Instead, the sustained popularity of Swift and the accompanying decline of Objective-C becomes clearer from market insights. Reports continued to attest to this in 2018, as job postings for Swift continue to rise. Demand for Objective-C is considerably less, as you can read in “Swift gains as iOS and macOS language, while Objective-C declines”.

Advantages of Swift

Swifts‘ popularity aside, there are several advantages if you move your app from Objective-C to Swift.

Reduced coding effort

Programmers need to code more in Objective-C for string control, class division, and string concatenation. Swift has introduced operators for these. Developers can use these operators, therefore, they save coding effort. Read more about this advantage in “Objective-C or Swift: which technology to learn for iOS app development?”.

Fast and performance apps

Swift is lightweight. Apple estimates that Swift apps are significantly faster than Objective-C apps, moreover, the advantage is increasing with every new release of Swift. Mobile apps are ’Systems of Engagement’ (SoEs).

Performance and quick response matters to mobile apps. This improved performance is a clear advantage that Swift offers. Read more about it in “Swift vs. Objective-C: 7 benefits of Swift development”.

Code safer apps with Swift

Swift helps programmers to code secure apps. The language has been designed to eliminate errors. Features like generics and type interference make the app stable. Swift apps are less prone to bugs and crashes. Learn more about these features of Swift in “Swift vs Objective-C. Which iOS language to choose”.

Better readability

Swift is an English-like language. Developers find it easier to learn the syntax. Readability of the app improves. Developers with Java, C, C#, C++, or Python skills can easily learn Swift. Find out more about it in “10 things I like about Swift”.

It goes without saying that the easier syntax and better readability help all developers in the team to understand code that others wrote. In turn, this improves team productivity.

The language of the future

As I have mentioned already, developers use Swift even outside iOS. Programmers are already coding apps for macOS, tvOS, and watchOS. This clearly makes the language relevant in a wider context, which signals a bright future for the language.

Robust support from technology giants

You already know that Apple is solidly behind Swift, however, they’re not the only technology giant supporting the language. IBM is another key technology giant that has offered robust support for the language.

IBM is already conducting research concerning Swift. This bodes well for the growth of the language. Read more about IBMs’ initiatives supporting Swift in “Swift: probably the best full-stack language in the world”.

Better maintainability

Swift offers better readability, moreover, it reduces coding. The combination of these two factors offers important advantages to programmers. It‘s easier to maintain a smaller code base, which is also easier to understand!

Better ’App store optimization‘ (ASO)

Apple rewards apps that use their latest technology. Provided you have a high-quality app, your chance of being featured on the ’Apple App Store’ improves if you use Swift. To understand the importance of the app being featured, read “App store optimization tips & checklist (2019 ASO guide for iOS & Google Play apps)”.

How to work with Swift?

You need to first know a bit more about Swift before you undertake an Objective-C to Swift conversion project. There are several repositories that feature comprehensive documentation.

Swift.org

Swift.org is an excellent starting point. Programmers can download Swift. There is a ’Getting started‘ guide, which takes you through installation, using the package manager, etc.

Swift.org features extensive documentation for the language. This includes a language guide and a language reference. The ’source code‘ webpage points you to the GitHub repositories of the Swift project, moreover, all are open-source repositories.

Read How We Helped a Marketing Company to Build a Back-Office Custom Ads Dashboard

Mobile apps are SoEs, therefore, developers use the ’Agile‘ methodology to create them. This requires iterative development. Continuous integration using automated build management, testing, etc. are important. Learn how Swift supports continuous integration here. There is also documentation about Swift 4.2.

Apple developer portal resources for Swift

Apple offers extensive resources for programmers to work with Swift. These are on their developer portal page for Swift. You can create small Swift programs called “playgrounds” where you can immediately check the results of your code. Learn how to do that on an iPad, here.

You can find a wide variety of information here. For e.g., it shows how you can use the “Read-Evaluate-Print-Loop” (REPL) shell with Swift. Read more about REPL here.

Xcode, the famous ’integrated development environment’ (IDE) of Apple improves the developer productivity. The developer portal documentation for Swift shows how to use it with Xcode. There is also detailed documentation about the framework.

How can you convert your project from Objective-c to Swift?

I will now describe the steps to migrate your project from Objective-C to Swift.

Step #1: Induct an Objective-C to Swift conversion team

I assume you will not change your ’user interface‘ (UI). Other assumptions are that you are not adding new features, and you will continue to use your existing ’Mobile Backend as a Service‘ (MBaaS) account.

You need a team of iOS developers with Objective-C and Swift skills. The team should also have testers. Finally, you need a project manager (PM). I recommend you form an Agile team, in which our guide “How to build an Agile development team?” can help.

Step #2: Identify the portions of the app for converting Objective-C to Swift

First, determine which part of your Objective-C code you will convert into Swift. You can leave some parts of the app with Objective-C code intact. Apple allows Objective-C and Swift code in the same app.

Following are a few examples of codes that you don’t need to convert to Swift:

  • 3rd party libraries;
  • Helper and extension classes;
  • Some Objective-C runtime aspects like ’Objective-C message send‘;
  • Any low-level code that depends on the C/C++ code.

Read more about this in “Should I convert everything to Swift?”.

Step #3: Prepare your Objective-C app for code porting

You should ensure that your Objective-C has optimal compatibility with Swift. To this, you need to first modernize your current Objective-C code base.

Does your current code base use nullability annotations? If not, you need to modernize it first. A modern Objective-C code base will interact better with Swift. Read “Migrating your Objective-C code to Swift” for more details.

Step #4: Objective-C to Swift conversion using tools

This is a key step in your project for migrating apps from Objective-C to Swift. There are several ways to accomplish this. You can use available tools for Objective-C to Swift conversion. Let‘s dive in!

Objective-C to Swift conversion tool #1: iSwift

iSwift lets you convert your existing Objective-C code to Swift. It’s an intuitive tool. They offer you a ’Swift 4-in-1 Pro bundle‘, which includes the iSwift conversion tool. It also includes a ’Swift cookbook‘ to aid you in Swift coding. Finally, there‘s ’Run Swift‘ to help you with testing.

Tool #2: Swiftify Objective-C to Swift converter

Swiftify has a tool to convert Objective-C code to Swift. It‘s easy to use. Web developers can use it, moreover, they provide an interactive playground to run Swift online. Developers can generate Swift documentation, and also learn Swift easily. Access their tool here. Contact them to know how much the tool will cost you.

Tool #3: ’Advanced Project Converter‘ from Swiftify

Swiftify also has an ’Advanced Project Converter‘, which is also called “ObjectiveC2Swift”. This has several advantages, for e.g., it comes with ’Swiftify for Xcode‘, which makes the code conversion easier. The ’Advanced Project Converter‘ has other features, for e.g., you can select a group of files to convert, add new Swift files to the project folder automatically, etc.

Read “Converting your project to Swift with Swiftify‘s advanced project converter”, which can guide you through the code conversion process. Check their pricing plans here.

Alternative step #5: Migrate your Objective-C code to Swift manually

This is an alternative to using a tool to convert your project from Objective-C to Swift. You can hire highly skilled developers. They can take a custom development approach.

To do so, your project team will need to build a new code base in Swift from scratch, after studying the Objective-C code. They will need to test it thoroughly. The team should do this on a file-by-file basis. This means that they will convert one ’class’ at a time.

Developers will need to create a new Swift ’class‘ using Xcode. They then import all relevant system frameworks. Subsequently, programmers will need to fill up the Objective-C bridging header, to get the Objective-C code for that ’class‘. Read “Convert Objective-C app to Swift with minimum effort” to find more detailed instructions.

Coders will need to make their new Swift class accessible to Objective-C. It should be usable with Objective-C. The code migration involves the Objective-C header files, then the Objective-C modules, and finally conversion into Swift APIs. Follow through with the implementation and troubleshooting to ensure a successful migration.

Step #6: Test the converted code

Whether you use tools for conversion, or manually convert the code, you need to test the app. Xcode offers a comprehensive UI testing framework for Swift where you can use test scripts.

The ’new project wizard in ‘Xcode guides you through the test, for e.g., it asks whether you will include unit tests and UI tests. You can also record the UI test. Read “Understanding UI testing using iOS, Xcode 9 and Swift” for guidance.

Planning a bigger Objective-C to Swift conversion project?

If you have a complex requirement of migrating apps from Objective-C to Swift, you might need professional help. The project might be too critical for your business. Swift is a premium skill, and the lead-time for hiring could be high. This may impact your project schedule, therefore, you may need to engage a software development company. Check our guide “How to find the best software development company?” to prepare for such requirements.

Download Our Project Specification Template