Dictionary

A dictionary stores a set of keys of a specific type, which are associated with values from a specific type (keys and values may have the same type, but this is not the case in general). The key feature of the dictionary collection is the association. The data is not ordered like in the set, but we have a powerful way to find data based on its key. Usually, a dictionary is a map which keeps all relations between the keys and the data items.

The key type should conform to the Hashable protocol. (See the Sets section from this chapter.)

Here is how we can define a dictionary in Swift:  

var animalsDictionary = Dictionary<String, String>(dictionaryLiteral: ("dog", ""), ("cat", ""))
var animalsDictionaryLiteral = ["dog": "","cat": ""]
//adding a new association
animalsDictionary["bird"] = ""
for association in animalsDictionary {
print("(association.key) -> (association.value)")
}

We can construct a dictionary using the default construction, defining the type of the key and the type of the values in < >, as in the preceding example as  Dictionary<String, String>() or the short type [String:String]. There is a short literal syntax for creating a dictionary. It's close to defining an array but every value is preceded by a key. For example: ["dog": "","cat": ""]. If you want to create an empty dictionary, you have to define its type and you can use the empty dictionary literal [:] or [<key_type>:<value_type]():

var emptyDict:Dictionary<Int, String> = [:]
var emptyMap = [Int: String]()

The dictionary object has a similar public interface to the array:

  • .count: A property, which is read-only and returns the number of items in the concrete dictionary instance
  • .isEmpty: A property, which is read-only and returns true if and only if the dictionary instance has no items
  • .updateValue(_:forKey:): This function can be used to assign a specific value to a specific key
Only a single value can be stored per unique key.
  • .removeValue(forKey:): A function which removes a key-value pair and returns the value, if such a key exists; if the key is not part of the dictionary, then nil is returned

Using subscripts, we can easily add, update, and check if a value is associated with a specific key. Thus, the type returned when accessing a specific key is the optional version of the type of the values.

We can use for...in to iterate over the dictionary data. Here are some examples:

//all pairs
for (animalName, animalEmoji) in animalsDictionary {
print("(animalName) -> (animalEmoji)")
}

//all keys
for animalName in animalsDictionary.keys {
print("(animalName)")
}
//all values
for animalEmoji in animalsDictionary.values {
print("(animalEmoji)")
}

We have easy access to all keys and all values. We can easily convert those to set or array collections:

var allEmojis = [String](animalsDictionary.values)
var allAnimals = [String](animalsDictionary.keys)

The following section discusses the data structure in general and their good and bad sides. There are some nice examples showing where each one should be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.37.136