Adam DeLong

Swift for Rubyists: Arrays

All of the examples from this post are available as a playground to download at this repo.

Update: I have corrected a mistake in this post which stated that mixed Swift arrays of Ints and Strings worked because they were inferred to be the AnyObject type. This was incorrect. This only works in cases where you have imported the “Foundation” library or if you use the type Any. My apologies. See this article for further explanation.

Arrays

Swift arrays are like arrays in most modern languages including Ruby. In some cases, they share some similar characteristics. In others, they are very different. We’ll compare how Swift handles arrays vs Ruby.

Swift Array Declaration and Instantiation

Due to type-safe nature of Swift, the declaration syntax of Swift arrays to Ruby arrays is similar, but the differences are very important.

Declarations

In Ruby, it couldn’t be simpler:

[]

In Swift, there are three main ways to declare an array. In the first two ways, you must declare the types that the array will hold. Here are the first two using an example of declaring an array to hold String types.

var swift_core: [String]
var swift_core_again: Array<String>

Literals

Luckily, Swift has a third option. They are literals and they are very similar to Ruby literals. The examples I use throughout this post will be mostly be literals. The syntax is almost identical.

So, if you try to use the Ruby shorthand for declaring and instantiating an array, you will get very different results in Swift.

If you wanted to do the above to create a Swift Array, you would use:

var arr = [String]()

Subtle, but important. There are actually ways to convert an NSArray to a Swift Array, but we won’t cover those in this post.

Let’s step back and talk about declaration vs. instantiation in Swift. Unless you are using a literal, Swift requires you to declare a type before instantiating an array. So, you actually have two steps if you decide not to use a literal. Declaring an array in Swift does not actually instantiate it. In this example, we declare a type in Swift, but do not instantiate it, so when we go to inspect the “count” property, we get an error.

Mixed arrays, such as those of Ints and Strings, can be achieved only when you have imported the “Foundation” library. For example, here’s a valid array that holds both Int and String types.

import Foundation
var inebriated_array = [99, "bottles", "of beer"]

This can be achieved because Swift is automatically converting the instances to NSStrings and NSNumber classes. Check out this article here for more information.

Type-safety also applies if you try to add an element which does not match the type of the existing elements of an already instantiated array. For example, if we try to add an element using Swift’s “#append” method, this will not compile:

var broken_up = ["Blink"]
broken_up.append(182) //compile error

Appending

Ruby and Swift are almost identical with regards to the syntax of how to add an element on to the end of an array. Here’s an example in Ruby of adding two arrays together.

a = ["Simon"]
a += ["Garfunkel"]

In Swift, it’s the same.

var band = ["Simon"]
band += ["Garfunkel"]

In Ruby, you can use “#push”, which returns the modified array so you can assign it to something else.

If you need the current index of the element that you are iterating over, similar to Ruby’s “#each_with_index” method. You would use the “#emunerate” function in the for..in loop. Each iteration of enumerate returns a tuple containing the index and the element. We’ll cover tuples another time.

More to Learn

This is just a brief introduction to Swift arrays for Rubyists. If you have found this information useful and would like to learn more, please sign up for the Swift newsletter below and I will send you updates when new posts and videos become available. You’ll also receive access to my video tutorial “Advanced Swift Arrays”.