Dictionaries (swift)

ref – http://jamesonquave.com/blog/swift-3-tutorial-fundamentals/

Dictionaries

Dictionaries are able to store values based on a key, typically the key is of type String, but it could actually be many different Swift types. In this example, we create a basic Dictionary with String keys and Int values for the age of each person:

We can access these values by their String keys:

result:
10
15

Note that we’re unwrapping these because they are optional values, and could potentially be nil. It is generally safer to use optional binding to unwrap the value from a Dictionary, especially if you have reason to believe the value could often be nil.

result:
Arya is 10 years old

We can also store arrays inside of dictionaries, or dictionaries inside of arrays, or a mix of both.

Here we have the dictionary like before. Except in this case, this dictionary is the value of key “Stark”, and key “Baratheon”.

In order to access, unwrap optional families[“Baratheon”]. It will give you an optional Dictionary. Unwrap it like this:

Then access the value at key “Tommen”, and the unwrap the optional int value.

Hence,

Tommen is 8 years old

The type of houses here would be [String: [String: Int]]
Or in other words it is a dictionary with a String key, and it’s values are [String: Int], another dictionary with String keys and Int values.

Sets

A Swift 3 Set is similar to an Array, except the values in a Set are unique and unordered.

Initializing a Set looks almost exactly like initializing an Array, the only different is the type:

This code creates a new set of type String. The greater than / less than symbols < and > are used to indicate Swift generic types, including the types of a Set as shown here.

You’ll notice we included “Blue” twice in our list, but if we print out the contents of colors, we only see it once:

result
[“Orange”, “Red”, “Blue”]

You may also notice that the ordering is inconsistent. Sets do not maintain any particular order for their contents.

We can not access members of a Set using indexes as we can with arrays, but instead we use the methods built-in to the Set type to add and remove objects. We can also call the contains method to check if the Set includes something.

result:

[“Black”, “Orange”, “Blue”]
true
false

Constructing sets of objects is a common way to catalogue what is included or excluded in a list of things, as long as there is no need to order or have duplicates of the objects.

Tuples

Tuples are not technically a collection, but instead simply multiple variables that can be passed around with a single identifier.

The type of the tuple here is (String, String), and we can manually access each numbered tuple element using dot-syntax, followed by the index:

result:
Quave
Jameson

Tuples can also be deconstructed in to new variable names:

Since we’re not using the last name here, we could just ignore that value by using an underscore _ and still deconstruct the first name:

let (first, _) = (“Jameson”, “Quave”)
print(first)
Jameson

Tuples are useful when you have a method that you want to return multiple values.