You’ve installed Xcode and are ready to get started with iOS development.
However, when you first create a project you’re met with a screen that asks what language? Objective-C or Swift?
The quick answer: You should learn Swift for iOS development, at least at first.
To understand why, let’s dive into the history books a little bit.
Objective-C was created in 1984, over 30 years ago. Through a series of acquisitions it was acquired by Apple in 1996. Objective-C is a thin layer on top of the popular programming language C.
Throughout 1996 to 2014, Objective-C was the main language used to build Mac and iOS apps. At Apple’s World-Wild Developer Conference in 2014, Apple introduced the new programming language Swift as “Objective-C without the C”.
Initially developers were hesitant and unsure that Swift would become the main development language for iOS development. However, over time, Swift has matured and become an amazing programming language for both beginner and advanced developers.
Swift officially passed Objective-C in measured popularity in 2018 and has been more popular for a while according to Google Trends.
TIOBE is a rating used to determine how popular a language currently is. It uses metrics such as popularity, tutorials, community, and jobs to determine this rating. As of December 2019, Swift currently ranks 10th, up from 14th in 2018.
Swift was made by Apple as a general-purpose, multi-paradigm language for Apple products, such as iOS, iPadOS, macOS, watchOS … etc.
Swift is also designed to be a safer language, avoiding null pointer errors with built in optionals. It also is more human-readable, aimed to improve the developer experience.
Swift also offers amazing performance and supports protocol extensibility. Apple promotes this as “protocol-oriented programming”.
Today Swift is the most popular language for iOS and MacOS development and Apple has continued to invest in the language every year after its release.
Swift is easier to learn.
Due to how modern Swift is, it’s easier to read and write than Objective-C. It’s true that once you’re experienced, both languages are easy to understand.
However, for beginners, Swift is designed to be safer and allows the novice developer to focus on concepts instead of syntax. The syntax in Swift is designed to closer to English.
Let’s take a look at some examples to learn why:
Functions are blocks of reusable code that can be accessed.
Here’s how you define functions in Objective C:
- (int) max:(int) num1 secondNumber:(int) num2 {
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
Here’s how to define a function in Swift:
func max(num1: int, num2: int) -> Int? {
var result: Int?
if num1 > num2 {
result = num1
} else {
result = num2
}
return result
}
Let’s take a look at how you call these functions…
In Objective-C:
[self max:a andNum2:b];
In Swift:
max(num1: a, num2: b)
In my opinion, Swift is easier to read and write for functions.
Let’s take a look at a different example…
Classes are code templates for creating instances or objects.
In objective-c, classes need to have two files, a header file (with the extension .h
) and an implementation file (with the extension .m
).
// .h file
#import <Foundation/Foundation.h>
@interface SampleClass: NSObject
- (int)max:(int)num1 andNum2:(int)num2;
@end
// .m file
@implementation SampleClass
- (int)max:(int)num1 andNum2:(int)num2 {
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
@end
In Swift, we only need one file. The class file looks like this:
class SampleClass {
func max(num1: int, num2: int) -> Int? {
var result: Int?
if num1 > num2 {
result = num1
} else {
result = num2
}
return result
}
}
From the above examples, we can see that Swift code is much easier to read, especially for beginners. This is not a surprise as Apple designed it this way.
Swift is now on version 5.1. It receives updates regularly to improve the developer experience.
This is unlike Objective-C, which had its last release (2.0) in 2006.
According to this stackoverflow survey Objective-C is a language that developers dread! Over 68% percent of objective-c developers are not interested in continuing to use this language.
Swift on the other hand is one of the most liked languages by developers. It comes in at 6th with over 69% of developers interested in continuing to use the language.
Although Apple originally designed Swift for MacOS and iOS applications, Swift has expanded greatly in recent years. It became open source in 2015 and this has increased adoption.
Backend server side Swift is now popular with Vapor. A popular framework that released version 4.0 this year.
Swift for Android was recently introduced in late 2019. This is a game-changer to write both iOS apps and Android apps in the same language.
Lastly, SwiftUI is a framework that allows you to build declarative user interfaces quickly for mobile apps.
It’s safe to say the Swift ecosystem has grown rapidly in the past couple of years and it doesn’t look like it’s slowing down.
Ok, so you should learn Swift first if you’re a beginner. But when should you learn Objective-C? Right after you learn Swift?
I recommend you learn Objective-C when required. This is common when you’re working for a company that has been around for 5+ years - remember Swift was only released in 2014. It’s common that you’ll have to work in a hybrid codebase consisting of old code in Objective-C and new code in Swift.
Another reason you could need to know Objective-C is if you’re working on older open source projects written in Objective-C. If you’d like to contribute, you’ll have to learn the language the code base is in.
Typically though, Objective-C should only be learned if its required. Most new iOS apps would be fine just completely coded in Swift.
Facebook is one of the largest companies that still codes their flagship Facebook app in Objective-C.
There are many speculations of why the big tech company hasn’t switched over to Swift, but it’s probably due to too much technical debt. Changing languages of a massive codebase is extremely expensive and has little to no benefit to the end user.
Also Facebook has built extensive tooling for Objective-C, making this older language feel more modern.
Since Facebook is the largest company developing with Objective-C still, naturally a lot of the frameworks are released by them. You can view all their open source Objective-C and Objective-C++ libraries on their github.
Here are some of the interesting ones:
So you’ve heard of React Native, Xamarin and Flutter. Frameworks that allow you to learn once and build for both iOS and Android.
I don’t recommend these for beginners - they’re newer and less stable that native app development. If Apple releases an update to iOS, these frameworks will take a while to update.
They also have more build issues as well, something you won’t want to deal with when you’re just starting out.
However there are cases where these frameworks make sense, for example in a small startup that requires both iOS and Android apps to be released.
Most people learning to code iOS apps should start with Swift. Learn Objective-C if your job or project requires it, but this may not be necessary.
Best of luck!
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.