What learning Swift looks like: Arrays

I’ve spent the last few months working on a new iOS application in Swift, Apple’s new language for Mac and iOS development.

I was constantly jumping into an Xcode Playground to play, test and break specific parts of the syntax. Going back through those snippet, while adding some commentary, was a good opportunity to show my learning process as I moved from scripting languages (JS and Python in particular) to type safe compiled language.

So for part one in this series I give you, Arrays.

Arrays, or lists or something..

Digging into how a language handles it’s built in Array type has always been interesting to me. It’s such an important part of a language that it often impacts what becomes idiomatic idiomatic code in language. Swift takes a relatively classical approach to the Array, but there were some interesting tricks to be found.

let x = [1, 2, 3]  
x.append(4)  
$ Immutable value of type ‘[Int]’ only has mutating members named ‘append’
let y = x.removeAtIndex(0)  
$ Immutable value of type ‘[Int]’ only has mutating members named ‘removeAtIndex’

Okay, I can’t add or remove from this array because Swift x is immutable (the let part). Immutable variables are at the core of the language so there aren’t MutableArray and ImmutableArray types like many languages have. I love this. The more that I use Swift the more I fall in love with immutable variables as such a core concept. I am so very glad that it has made it into the ES6 specification.

var x = [1,2,3]  
let y = x.removeAtIndex(0)  
$ 1
x  
$ [2,3]

There we go.

let t = [1,2,3,4] + [5,6,7,8]  
t  
$ [1,2,3,4,5,6,7,8]

Very happy that + is used for concatenation and not some horrible mess like JavaScript or a boring type error.

let z = [1, 2, 3, 4]  
z[0…2]  
$ [1, 2, 3]

Okay, that’s the syntax for an array slice. I’m glad there is some sugar for this. Let’s play some more.

let t = [1,2,3,4,5,6,7,8]  
t[0…20]  
$ bork

Hmm, there aren’t 20 items so it doesn’t want to give me anything. Strangely, Xcode doesn’t like this at all. It won’t give an error but it’s an Array Index error when you attempt to compile it.

let max = t.count < 5 ? t.count - 1 : 5  
t[0…max]  

Meh. It’s long, but it works. I haven’t found anything better yet either.

let z = [1,2,3]  
z.last  
$ 3
z  
$ [1,2,3]
z.pop()  
! ‘[Int]’ does not have a member named ‘pop’

Wait, I can get the last element but I can’t remove it? Not without supplying an index? That’s annoying. I wonder if I can fix that…

extension Array {  
    mutating func pop() -> T {
        return self.removeAtIndex(self.count - 1)
    }
}

One of the nicest features in Swift is the ability to extend any class with properties or methods using the extension syntax. In this case I also added mutating to the func declaration so that it’s only usable on mutable instances. The rest is easy, pull the last index off and return it.

Let’s try that new pop function we added to all Arrays

let z = [1, 2, 3, 4]  
z.pop()  
$ Immutable value of type ‘[Int]’ only has mutating members named ‘pop’

Ugh, I forgot.

var z = [1, 2, 3, 4]  
z.pop()  
$ 4

Nailed it.

Hopefully that code gives you a taste of what the Array can do. Next round is going to cover the static typing parts of Swift in a lot more detail.