Using dictionaries in swift?

Example:


  var ages = ["John": 28, "Doe": 32]
  print(ages["John"] ?? "Unknown")
  

Dictionaries store associations between keys and values. The example shows a dictionary with names as keys and ages as values.

Solution:


  ages["Jane"] = 25
  ages["Doe"] = nil
  for (name, age) in ages {
      print("(name) is (age) years old")
  }
  

We add a new key-value pair, remove a pair, and then loop through the dictionary to print each person's age.

Beginner's Guide to Swift