Apple’s new Swift language explained: A clever move to boost iOS, while holding Android apps back
While
Apple’s WWDC keynote yesterday was full of exciting new changes and
features, one piece of news caught everyone off guard: With iOS 8 and OS
X 10.10, and the the latest version of the Xcode developer tools,
Apple has introduced a whole new programming language called Swift.
According to Apple, Swift will make it a lot easier and more fun to
develop apps for both iOS and OS X — in contrast to the current
language, Objective-C, which has been likened by esteemed programmers to
pulling teeth. Swift will also apparently bring a significant
performance boost over Objective-C programs.
While Apple is being
fairly coy about the exact reasoning behind the launch of Swift, it’s
probably to reel in more developers, who will then create more apps,
ensuring the continued dominance of iOS app ecosystem. But hey, we’re
getting ahead of ourselves: What
is the Swift programming
language, anyway? And how can switching to a new programming language
provide a massive 50% performance boost over a language that is already
considered to be pretty fast?
What is Swift?

A small example of some Swift source code
For
a start, the Swift language bears no relation to the Swift (A6)
CPU architecture or the existing Swift parallel scripting language.
Obviously, when Apple decides on a name for a product, it doesn’t let
existing products or trademarks get in the way.
In the words of
Apple, Swift is like “Objective-C without the C.” The introduction to
Swift on the Apple developer website outlines Swift as safe, concise,
and interactive (your code is interpreted and rendered in a
live “Playground” view in the Xcode IDE).
In this context, “safe” mostly refers to the fact that the language is type safe — but thanks to type inference,
type declarations are less onerous, making Swift more concise than C or
Objective-C. The Switch statement is also is also safer and easier to
use than the C counterpart. The two-phase initialization process for
classes, slightly tweaked from the Objective-C way of doing things, also
improves safety. Along with type inference, Swift also introduces very
concise closures (lambdas).
A larger Swift source code sample, this time showing the Switch statement. It’s fairly human-readable, even for non-programmers.
On
the compilation and runtime side of things, Swift targets the same
Cocoa (OS X) and Cocoa Touch (iOS) APIs, and uses the same LLVM as
Objective-C. Swift code can co-exist with Objective-C code in the same
project, encouraging adoption.
A deep analysis of Swift is beyond
the scope of this story, but in general it shares a lot of similarities
with other modern languages, such as Rust, with a lot of popular ideas
and patterns that have been assimilated from other languages. There will
be a lot of cries that Apple copied/imitated/ripped off other languages
— but ignore them. When it comes to programming languages, this kind of
imitation and embrace-and-extent evolution is the norm, and a sign that
everything is working as intended.
Swift performance, compared to Python and Objective-C
Will Swift apps be faster than Objective-C apps?
On
stage at WWDC, Apple’s Craig Federighi showed some interesting graphs
that appeared to show a huge 40-50% performance lead for Swift over
Objective-C. He did not say that apps written in Swift would be faster
than Objective-C, though. And he was very picky about which benchmarks
he showed. In reality, Swift is very unlikely to be significantly faster
than Objective-C. They are both statically typed, compiled languages —
using the same LLVM compiler, no less. To obtain such a graph, Apple
probably had to choose an Objective-C feature that is known to be
slow/buggy — or intentionally optimize a Swift feature, purely for the
sake of producing a pretty graph.
We look forward to doing some
real benchmarking in the coming weeks and months, though, as Swift apps
start pop up on the App Store.
Why did Apple release Swift?
It
all boils down to this question. Why, with a huge pool of developers
who are already very knowledgeable in the ways (and quirks) of
Objective-C, is Apple releasing a new programming language? Doesn’t the
world already have enough programming languages? Why didn’t Apple choose
another modern language, like
Rust or
Go?
There
are a variety of possible answers, depending on how cynical you are.
The nicest explanation is that Objective-C is a horrible language to
learn — and so the introduction of Swift will massively increase the
number of developers who are happy and willing to develop iOS and OS X
apps. (The counterpoint to this is that we may then see a lot more junk
apps in the App Store.)

The LLVM logo
A
more complete explanation is that Objective-C is an old language — it
just had its 30th birthday — and so it does make sense to introduce a
newer language that more closely conforms to the modern methods of app
development. As a corollary to this, Apple is the creator and curator of
Swift, meaning it has a lot more flexibility to add and change
functions as time goes by, rather than being forever shackled by the
Objective-C framework. Think of it as an investment in the future.
The
cynical explanation is that Apple wants to use its heft to weaken the
Android app ecosystem. Thanks to their shared support for C, and tools
to port Objective-C to Java, it’s currently fairly easy to port an app
from iOS to Android. Porting Swift apps to Android, on the other hand,
would be a lot more time consuming.
In reality, it’s probably a
mix of all three reasons. Ultimately, there are a lot of good reasons
for creating your own, tailor-made programming language. The difficult
bit is in getting people to use and embrace the new language. Apple,
with its captive army of millions of developers, certainly won’t have
that problem.