Overview
- A collection whose elements are key-value pairs.
- A dictionary is a type of hash table, providing fast access to the entries it contains.
- Each entry in the table is identified using its key, which is a hashable type such as a string or number.
- You use that key to retrieve the corresponding value, which can be any object.
- In other languages, similar data types are known as hashes or associated arrays.
1. Creating a Dictionary
1.1 init()
- Creates an empty dictionary.
1.2 init(uniqueKeysWithValues:)
- Creates a new dictionary from the key-value pairs in the given sequence.
1.3 init(_:uniquingKeysWith:)
- Creates a new dictionary from the key-value pairs in the given sequence, using a combining closure to determine the value for any duplicate keys.
1.4 init(grouping:by:)
- Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
2. Inspecting a Dictionary
2.1 isEmpty
- A Boolean value that indicates whether the dictionary is empty.
2.2 count
- The number of key-value pairs in the dictionary.
2.3 capacity
- The total number of key-value pairs that the dictionary can contain without allocating new storage.
3. Accessing Keys and Values
3.1 subscript(_:)
- Accesses the value associated with the given key for reading and writing.
3.2 index(forKey:)
- Returns the index for the given key.
3.3 first
- The first element of the collection.
3.4 keys
- A collection containing just the keys of the dictionary.
3.5 values
- A collection containing just the values of the dictionary.
4. Adding Keys and Values
4.1 updateValue(_:forKey:)
- Updates the value stored in the dictionary for the given key, or adds a new key-value pair if the key does not exist.
4.2 merge(_:uniquingKeysWith:)
- Merges the given dictionary into this dictionary, using a combining closure to determine the value for any duplicate keys.
4.3 merge(_:uniquingKeysWith:)
- Merges the key-value pairs in the given sequence into the dictionary, using a combining closure to determine the value for any duplicate keys.
4.4 merging(_:uniquingKeysWith:)
- Creates a dictionary by merging the given dictionary into this dictionary, using a combining closure to determine the value for duplicate keys.
5. Removing Keys and Values
5.1 removeValue(forKey:)
- Removes the given key and its associated value from the dictionary.
5.2 removeAll(keepingCapacity:)
- Removes all key-value pairs from the dictionary.
6. Comparing Dictionaries
6.1 ==(::)
6.2 !=(::)
7. Iterating over Keys and Values
7.1 forEach(_:)
- Calls the given closure on each element in the sequence in the same order as a for-in loop.
7.2 enumerated()
- Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.
8. Finding Elements
8.1 contains(where:)
- Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
8.2 first(where:)
- Returns the first element of the sequence that satisfies the given predicate.
8.3 min(by:)
- Returns the minimum element in the sequence, using the given predicate as the comparison between elements.
8.4 max(by:)
- Returns the maximum element in the sequence, using the given predicate as the comparison between elements.
9. Describing a Dictionary
9.1 description
- A string that represents the contents of the dictionary.
9.2 debugDescription
- A string that represents the contents of the dictionary, suitable for debugging.
9.3 customMirror
- A mirror that reflects the dictionary.
Next - Classes and Structures by Example