Understanding dictionaries
Let's say you're writing an Address Book 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:
All keys must be of the same type and must be unique. All values must be of the same type, but not necessarily unique. Keys and values don't have to be of the same type as each other. 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 now look at how to work with dictionaries. You'll create a dictionary by assigning a value to it in the next section.
Creating a dictionary
Imagine that you're creating an Address Book app...