Key Concepts

Review core concepts you need to learn to master this subject

Dictionary

var dictionaryName = [ "Key1": "Value1", "Key2": "Value2", "Key3": "Value3" ]

A dictionary is an unordered collection of paired data, or key-value pairs.

Dictionaries
Lesson 1 of 1
  1. 1
    Data often comes in pairs. On our phones’ contact list, every name is associated with a phone number. When we go shopping for an item, it is marked with a price. Searching up a word in a dictionar…
  2. 2
    We can create dictionaries that are initialized without any key-values pairs in it. A dictionary with no key-value pairs is also known as an empty dictionary. This option is useful when we want …
  3. 3
    We also have the option of creating dictionaries that are initialized with elements. In a dictionary, an element refers to a single key-value pair. To create a populated dictionary, we will use di…
  4. 4
    When writing code, less is more. Programmers often try to keep their code as concise as possible to make their programs readable and efficient. Using type inference, the compiler can automatica…
  5. 5
    If we wanted to add a new key-value to a dictionary, we could use the following subscript syntax: dictionaryName[NewKey] = NewValue It is important to note that when adding new dictionary elemen…
  6. 6
    Sometimes we need to update the values of our key-value pairs to ensure they contain relevant information. For example, if one of our contacts changes their phone number, we can go into our setti…
  7. 7
    There are several options for removing key-value pairs from a Swift dictionary. In Swift, the keyword nil describes something as being empty or having no value. If we were to set the value of a ke…
  8. 8
    We’ve used Swift’s built-in properties, .isEmpty and .count, to gather information about arrays and sets. Since dictionaries are also a collection type, we can use these properties with dictionari…
  9. 9
    We’ve covered how to manipulate our dictionary and we can certainly print out the entire dictionary, but what if we want to access a single key-value pair? After all, we shouldn’t have to read thr…
  10. 10
    We can use loops to iterate through all of the elements of a dictionary! This is useful when we want to go through each individual key-value pair in a dictionary. For example, if we were the owne…
  11. 11
    What if we don’t need to loop through both the keys and the values of a dictionary? We can use the properties .keys and .values to create collections of the keys and values of a dictionary that we …
  12. 12
    Great job! In this lesson, we went over the following concepts: * A dictionary is an unordered collection of key-value pairs. * A key is a unique identifier used to create, update, remove, or acces…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo