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:
1 |
let ages = ["Robb": 15, "Sansa": 12, "Arya": 10, "Jon": 15] |
We can access these values by their String keys:
1 2 |
print( ages["Arya"]! ) print( ages["Jon"]! ) |
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.
1 2 3 |
if let aryasAge = ages["Arya"] { print("Arya is \(aryasAge) years old") } |
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”.
1 2 3 4 |
let families = [ "Stark": ["Robb": 15, "Sansa": 12, "Arya": 10, "Jon": 15], "Baratheon": ["Joffrey": 13, "Tommen": 8] ] |
In order to access, unwrap optional families[“Baratheon”]. It will give you an optional Dictionary. Unwrap it like this:
1 |
families["Baratheon"]! |
Then access the value at key “Tommen”, and the unwrap the optional int value.
1 |
families["Baratheon"]!["Tommen"]! |
Hence,
1 2 |
let tommensAge = families["Baratheon"]!["Tommen"]! print("Tommen is \(tommensAge) years old") |
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:
1 |
let colors: Set<String> = ["Blue", "Red", "Orange", "Blue"] |
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:
1 2 |
let colors: Set<String> = ["Blue", "Red", "Orange", "Blue"] print(colors) |
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.
1 2 3 4 5 6 7 |
var colors: Set<String> = ["Blue", "Red", "Orange", "Blue"] // blue, red, orange colors.insert("Black") // black, blue, red, orange colors.insert("Black") // black already exist colors.remove("Red") // black, blue, orange print(colors) print(colors.contains("Black")) print(colors.contains("Red")) |
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.
1 |
let fullName = ("Jameson", "Quave") |
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:
1 2 3 |
let fullName = ("Jameson", "Quave") print(fullName.1) print(fullName.0) |
result:
Quave
Jameson
Tuples can also be deconstructed in to new variable names:
1 2 3 |
let (first, last) = ("Jameson", "Quave") print(first) Jameson |
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.