Start with Swift! – 02: Complex types

Welcome to Start with Swift! This is a “series” of articles to get to know the basics of the Swift programming language. Want to start programming? Swift is an ideal choice to start with because it is expressive and easy to understand.

Now let’s get started. In this article we will look at complex types. They store more data, that’s why they are complex. But they actually make coding simpler.

Table of Contents

1. Arrays

Arrays are the most common when it comes to storing values. The order of items matters. They look like this:

let colour1 = “red”

let colour2 = “green”

let colour3 = “blue”

let rgb = [colour1, colour2, colour3]

You can recognise arrays by brackets with items inside it. Each item must be separated by comma (,). The program can read values from an array and it counts from 0. So if you want to read “green”, you would write this:

rgb[1]

NOTE: Swift crashes if you’d want to read item, that doesn’t exist, for example: rgb[6] in our case.

2. Sets

Sets are similar to arrays, except the items aren’t stored in any order. Also items can’t appear twice in the set. If so, the duplicate will be ignored.

let colours = Set([“red”, “blue”, “green”])

You must include both brackets.

3. Tuples

Tuples can store more values at once. You can access items by using numerical positions or by naming them. They have a fixed size, that means you cannot add or remove items from them. You can’t also change the type of items.

var name = (first: “Steve”, last: “Jobs”)

Now, you can access for example first name using:

name.0

or

name.first

SUMMARY: Arrays, Sets and Tuples are quite similar at first, but when you summarize them, they have different syntax and usage. So let’s look at them again:

If you need specific and fixed collection of values, where each item has a precise name or position -> use Tuple.

If you need a collection that must be unique or if you want to check whether specific item is included -> use Set.

If you need a collection that can contain duplicates or the order of items matters -> use Array.

4. Dictionaries

let quantity = [

“apple”: 1

“mango”: 2

]

“apple” and “mango” are identifiers and they are called “keys” in Swift. If you want to read data back, you’d type:

quantity[“mango”]

DEFAULT VALUES: Their usage is to cover all possibilities. Let’s look at them:

let favouriteFruit = [

“Paul”: “pineapple” ,

“Sophie”: “strawberry”

]

But if we want to read George’s favourite fruit:

favouriteFruit[“George”]

We will get back nil which means nothing. It’s because we didn’t provide a value for that key (George). Here’s how we can fix this:

favouriteFruit[“George”, default: “Unknown”]

5. Enumerations

Enumerations are used to define groups of similar values in a way, that makes them easier to use. The shortcut for this command is “enum”.

enum Activity {

case bored

case talking(topic: String)

case singing(volume: Int)

}

let conversation = Activity.talking(topic: “holidays”)

These are so called “associated values”. There are another one called “raw values”:

enum Planet: Int {

case mercury = 1

case venus

case earth

case mars

}

From now on, Swift will count the planets upwards meaning that Earth is now the third planet. If you wouldn’t add the specific number from which Swift would have to count from, it will count from 0 by default.