Swift Basics — Week 4
Collection Types
📅 Last week
We talked about the definition of a data type:
Data type: — The type of information that is stored in a variable
This week were talking about a very specific type of data type, a Collection Type. But first! Let’s review the homework.
📚 Homework Review
Create all of the data types you know so far
One possible solution:





Try removing the other types and see what Swift guesses! Moving on to the new concepts.
💭 Loops: (The Concept, without code)
There are special data types called Collection Types.
Collection Type: — A constant/variable that holds a collection of constants/variables
There are two types of Collection Types we’ll be
Array: — An ordered collection of constants/variables, each item in the collection is represented by a number
Dictionary: — An unordered collection of constants/variables, each item in the collection is represented by a string
What do Collection Types do for me?
They make it possible to collect multiple variables together under one name. You can then do something to all or just some of the variables.
It’s much easier to explain what they are in code.
💻 Collection Types: (Code Examples)

We made an Array that contains the integers, 1, 2, and 3.
We made a Dictionary that holds three pairs. The first of each pair is an English word. And the second part of the pair is the Spainsh translation of that English word.
An array or a dictonary is surrounded by brackets “[ ]” and each item in an array or dictionary is seperated by commas.
The special part about a dictionary is that each pair is grouped together by a colon “:”.

So we created an array and a dictionary, but how do we use them?
Example: — Pulling values from an Array


I know this is weird and doesn’t make sense. But in the programming world, someone decided that arrays start at zero.




Example: — Pulling values from a Dictionary


Conclusion
Collection Types will be an essential part to every project you start. It’s important to experiment with them. Have fun, try testing out arrays and dictionaries
📒 Homework
Create a Phone Book
- Create a dictionary named “phoneNumbers”
- Inside of a function, add your family and friends names with their associated phone numbers attached
- Pull your mom’s number out from the dictionary
- Call her on the phone
If you have any feedback for me tweet me at @ryancortez. I’ll see you next week. 😉