From zero to a Swift(y) hero

I’ve wanted to learn how to build iOS applications for quite some time. Unfortunately, Objective-C never compelled me and I’ve used this as an excuse not to start.

Apple introduced a new programming language little over a year ago, called Swift. So what is Swift? Well, let’s see what Wikipedia says:

Swift is a multi-paradigm, compiled programming language created by Apple Inc. for iOS, OS X, and watchOS development. Swift is designed to work with Apple’s Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C (Obj-C) code written for Apple products. Swift is intended to be more resilient to erroneous code (“safer”) than Objective-C, and also more concise.

Great, that means that my main objective to not pursue a career in iOS development was gone. I could use Swift! I began reading more about Swift and iOS development in general and quickly realized that I found it all really interesting.

I’ve now worked with Swift for about a week and I thought I’d share things I’ve found interesting along the way. So, without further ado, let’s talk about closures!

 Closures

In Swift, functions are first-class citizens. This also means that Swift support closures. Closures are a really powerful concept and I’ve come to really enjoy working with them in Swift.

Let’s say we have an array of names.

let names = ["Kevin", "Alfie", "Linus"]

and we want to sort these names in reverse alphabetical order. To accomplish this we could use the sorted function. It takes one argument, a function, that in turn takes two String arguments and return a Bool value. Lets try it out!

func sortBackwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}

names.sorted(backwards) // #=> ["Linus", "Kevin", "Alfie"]

It worked! Now, lets rewrite the same functionality using closure expression syntax.

names.sorted({ (s1: String, s2: String) -> Bool in return s1 > s2 })

This will yield the same result as the first example, but we didn’t have to create a named function in order to do so. This is all good, but we can improve upon this even further.

Since we’re passing the closure as an argument to the sorted function, Swift can use type inference to determine the types of its parameters as well as the type of the value it returns. Knowing this, we can rewrite the above example as follows.

names.sort({ s1, s2 in return s1 > s2 } )

This looks neat! We’re still achieving the same result and the indent of the expression is very clear. This is a good thing. But wait, we can do more!

Since this is a inline closure, we can omit the return statement and let the closure implicitly return.

names.sort({ s1, s2 in s1 > s2 })

We can also make use of Swift’s shorthand argument names since this is a inline closure. That is, we can refer to the value of s1 as $0 and the value of s2 as $1.

names.sort({ $0 > $1})

Compare this to the first example. Quite amazing, isn’t it. We could go on and make this even shorter but I’ll leave that for your own exploration.

If you’re interested in Swift and its closures I highly recommend to read Apple’s own guide on closures covering these examples in greater detail as well as addressing topics such as capturing values from the surrounding context and how closures actually are reference types in Swift.

Happy coding!

 
9
Kudos
 
9
Kudos

Now read this

Vim plugin manager (vpm)

Two years ago I wrote a simple command line tool called vpm. A standalone plugin manager for Vim that was not integrated with the editor itself. It was created because I’m a strong believer in the Unix philosophy of doing one thing and... Continue →