Five reasons to start using Swift

By Michael Argentini
Managing Partner, Technology and Design

As an iOS developer I have to admit to being conflicted about using Apple's new programming language, Swift. It looks really great, but it's a big time investment to squeeze into an already busy schedule. I'm sure Objective-C will be around for a long time (unfortunately?), so there's no rush.

And so I languished in this land of indecision.

Hobby projects to the rescue! Recently I was working on a pleasure project that leveraged the YouTube API, and had reached a point where the development was going to go beyond my functioning proof of concept. After some reflection, I decided to rebuild it in Swift.

And I'm so glad I did.

I quickly discovered why using Swift is so great, even through the sometimes tedious process of learning a new language. In my opinion, the following five reasons to use Swift should convince you too.

1. Playgrounds

This is possibly the best reason to use Swift. Shown at the Apple announcement, Playgrounds allow you to write code and see results instantly. Each line is evaluated and output is generated. You don't have to create an Xcode project, and the entire playground is saved as a single file. You can create as many playgrounds as you like.

Playgrounds are a great way to test blocks of code, classes, functions, and more, without the baggage of an Xcode project. They're like a console on steroids. You could write something simple, or complex. You could even write a fully functioning application.

2. The Language is modern and refined

Okay, this section could get lengthy as I'd like to cover my favorite features of the language and its syntax. The problem is that I'm really fond of so much of it. So I'll try to keep it brief. Bear with me.

Strangely familiar

Any seasoned developer who first looks at Swift will immediately find it familiar. That's no surprise, given the inspiration from other languages that drove its development.

  • Optional semicolons; from languages like JavaScript and Python
  • Data structure declarations; from languages like C# and Java
  • Inferred data types; from languages like JavaScript
  • Dictionaries; hash tables from languages like JavaScript
  • String templating; from languages like Cold Fusion and JSP
  • Tuples; from languages like Lisp and Python
  • Protocols; similar to interfaces in languages like C# and Java
  • Signed and unsigned integers; from languages like C# and many others
  • Closures; from languages like Lisp, and similar to Objective-C Blocks
  • Automatic Reference Counting (ARC); similar to garbage collection in languages like Java and C#, and also available with Objective-C

All this aside, the most significant factor for familiarity is that the overall syntax is very C-like, as is the syntax of many modern languages.

Variables and constants

Swift really embraces constants for their performance gains and efficiency in memory use. But that doesn't mean that variables aren't important. They are. But you'll undoubtedly be using constants significantly more than you ever have in the past. One reason is that you can define a constant without a value, like a variable. Once it is assigned a value, it becomes immutable. And if you think about it, that's a very common use case for variables.

Lessons learned from Objective-C have given Swift a native array data type. They're zero-based, and typed, which eliminates the need to check element object types as they are iterated. And yes, you can declare an array of constants, with or without initial values.

One last thing regarding variables... Swift data types can be nullable (called optionals), much like C#, providing an easy way to determine if a variable has even been initialized at all.

Smarter defaults and code safety

One of the great things about Swift is that everything about a programming language was re-evaluated, including default behaviors and assumptions. So many of the annoyances of other languages have been fixed.

Take, for example, the switch statement. The C language version of this logic statement allows evaluations to fall through by default, as in the following C example:

int x = 0;

switch (myVar) {
    case 0: x = 3; break;
    case 1: x = 4; break;
    case 2: x = 5; break;
    case 3: x = 6; break;
    case 4:
    case 5:
    case 6: x = 7; break;
    default: break;
}

For most use cases, there will be a lot of break statements. In Swift, the default is to always break, unless you tell it to fall through by using a range operator. Here's the Swift equivalent of the previous example:

var x = 0

switch myVar {
    case 0: x = 3
    case 1: x = 4
    case 2: x = 5
    case 3: x = 6
    case 4...6: x = 7
    default: break
}

In the case of switch statements, you are required to be exhaustive in your comparisons. Generally that means you must have a default comparison. But that's okay. It will help to reduce unexpected behavior in your code.

And one of my favorite requirements is that all if statements require braces. So this is invalid:

if (x > y) a = b;

You'd use this instead:

if x > y {
    a = b
}

It's more readable, and much safer code. Remember Apple's OpenSSL goto fail flaw? That would have been impossible if it had been written using Swift syntax.

Brevity

One of the shortcuts that Swift provides is a reduction in the number of characters within your code. In most cases where parentheses are required in C-based languages, none are required in Swift (like if, while, and for statements). They won't break anything if used, but are generally leveraged to group logic clauses.

Semicolons are also optional. So, using them won't break anything, but they are required if you want more than one statement on a single line.

Tuples

Tuples are a great way to group multiple values for all kinds of uses, like passing to a function or object, and even returning from a function (akin to method output parameters in C#). In the example below, you can see a simple function that returns a tuple:

func getCityAndState() -> (city: String, state: String) {
    // Do some work...
    return ("Philadelphia", "PA")
}

let (city, state) = getCityAndState()

MIND:BLOWN.

3. More manageable code

Swift eschews the legacy of Objective-C header files (distinct compilation units) in favor of a more unified compilation (much like C# and other languages). That means variable declarations and such are contained within their code files, and you've effectively halved your file count. This makes managing a project significantly easier, and coding faster.

4. Speed

Do a little digging and you find that many reputable sources are benchmarking compiled Objective-C and Swift code and finding that Swift is almost always faster, and in some cases, by orders of magnitude. Apple's own video course uses code to generate a Fibonacci sequence to show just how efficient and performant Swift is versus Objective-C.

Take this with the proverbial grain of salt. You could code horribly in Swift and allow Objective-C to run circles around your code. But all things being equal, if best practices are followed, this is not the case. The latest version of Swift (1.2) has almost doubled its performance in many key areas as well, so the optimization and maturation of the language is ongoing at a fast pace.

5. It's the future of OS X and iOS development.

Swift is clearly the future OS X and iOS development language. There will be a day when Objective-C is retired or effectively inadequate. When this will be the case is up for debate. But learning Swift now certainly mitigates that inevitability.

So get cracking.

Article last updated on 4/21/2018