Let's say you're writing a contacts app. You would need to store a list of names and their corresponding contact numbers. A dictionary would be perfect for this.
A dictionary stores key-value pairs in an unordered list. Here's what it looks like:
Key | Value |
Key 1 | Value 1 |
Key 2 | Value 2 |
Key 3 | Value 3 |
All Keys must be of the same type and must be unique. All Values must be of the same type. Keys and Values don't have to be of the same type, though. You use the Key to get the corresponding Value.
If you create a dictionary using the let keyword, its contents can't be changed after it has been created. If you want to change the contents after creation, use the var keyword.
Let's look at how to work with dictionaries. You'll start by creating a dictionary.