There are two native programming languages for iOS—Objective-C and Swift. This becomes abundantly clear as soon as you start your first project in Xcode. After pressing to create a new application, you are taken to a page where you need to name your new project. You also have to pick a language, either Objective-C or Swift, to start you project. Why should you pick one versus the other? If you’re just starting out, which should you learn first? To start to get a better picture, let’s first look at some of the history of the two languages and what their advantages/disadvantages might be.
Brad Cox and Tom Love designed the Objective-C language in the early 1980s at a company called StepStone. This new language was layered on top of C, which means that extensions were added to C that enabled objects to be created and manipulated. NeXT Software licensed Objective-C in 1988 and developed libraries and a development environment called NEXTSTEP (Steve Jobs was the CEO of NeXT). Apple Computer acquired NeXT Software at the end of 1996 (allowing Jobs to return as CEO), and their NEXTSTEP/OPENSTEP environment became the basis for the newest OS X operating system. Apple named their version of the development environment Cocoa. This is a short version of how Objective-C came to be the native language for Apple development tools, specifically Xcode, using the Cocoa API.
Development of the Swift language began at Apple in 2010 by Chris Lattner. Outside developers first received a version of the programming language on June 2, 2014 at Apple’s WWDC conference. Swift 1.0 was released for Xcode 6.0 for iOS on September 9, 2014. Swift 2.0 and 3.0 were released in September 2015 and 2016, respectfully. The Swift language was originally a proprietary language to Apple, but it was made open source with version 2.2, released in December 2015. Swift is designed to work with Cocoa and Cocoa Touch, as well as the existing Objective-C code written for Apple products. The language is intended to be safer and more concise than Objective-C.
Benefits of Swift
Let’s take a look at some of the benefits of using the Swift language. First off, if you’re first learning a language, the basics of swift are easier for beginners. The language is modern (compared to Objective-C being 30+ years old), more readable, less verbose, and has less technical formatting (semi-colons to end lines among other things). The language also has a more engaging coding environment for learning. Apple introduced Xcode Playgrounds on Mac and Swift Playgrounds on iPad, which are both interactive environments that make learning the language more fun and give you immediate feedback.
Next off, the language is designed to be safer than Objective-C. This is due to Swift’s strong typing system and error handling. The use of optionals, and the techniques that go with them, help prevent errors of null pointers and prevent pyramid of doom situations. Of course, incorrect use of forced unwrapping can lead to issues in Swift, but it is designed to be safer if coded properly.
On top of these features, Apple is committed to Swift. They invested a considerable amount in making Swift the language of the future for Apple development. They quickly iterated new versions and worked to increase adoption of the language in the community. Swift usage is rapidly increasing and will likely be equal or greater to Objective-C usage in open source projects in the next 3 years if current adoption rates continue. Swift currently processes faster than Objective-C and is expected to get even faster in the future.
Pitfalls of Swift / Benefits of Objective-C
Although Swift is likely the language of the future for iOS development, there are a number of reasons why it’s not the clearcut answer just yet.
To begin, every update to the Swift language requires updating your code to the new version. Xcode does provide a tool to help assist in updating your code, but this often doesn’t fix all of the issues. This can be a time consuming process and a headache that comes along with each new version. If you have a very large project or a project for a client that won’t be carefully updated, this might be more than you care to deal with.
From a coding standpoint, Xcode currently does a better job with code completion and realtime error marking with Objective-C. This isn’t a big issue, but it can make for faster and easier coding with Objective-C by using their autocomplete suggestions.
If you need to support iOS operating systems prior to iOS 7, Objective-C is your only choice. Prior operating systems do not support Swift.
Finally, several APIs work much more naturally with an Objective-C codebase. For some Foundation APIs like AVFoundation and CoreAnimation, there are Swift wrappers that are OK, but these are C APIs that work smoother and with better control when writing in Objective-C. You can also utilize C++ libraries and use cross-platform C++ SDKs with Objective-C. Implementation files with the suffix .mm allow you to code in Objective-C++ as well.
This is a short overview of the two languages that is designed to highlight high level features of them rather than get into detailed technical nuances. There are certainly positives and negatives of coding in Objective-C and Swift. To add even more to the equation, Objective-C and Swift can be used in the same project through a process called bridging. My personal opinion is that right now, developers really need to know/learn both languages. You can make the case that either is better for an individual project, and I think that’s definitely true. What language you are more comfortable with, your team makeup, the size of the app, the APIs that you need to use, and the app’s overall goal all play an important role in deciding what language is the best for your particular project. I do believe that Swift is the language of the future, but both languages clearly are part of the current world of iOS app development.
These sites and book are great resources on the topic and were very helpful in writing this blog post. Please visit them if you wish to further explore the topic.
Programming in Objective-C, Fourth Edition by Stephen G. Kochan