StructureSwift5.9.0
Dictionary
A collection whose elements are key-value pairs.
@frozen struct Dictionary<Key, Value> where Key : Hashable
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.
Create a new dictionary by using a dictionary literal. A dictionary literal is a comma-separated list of key-value pairs, in which a colon separates each key from its associated value, surrounded by square brackets. You can assign a dictionary literal to a variable or constant or pass it to a function that expects a dictionary.
Here’s how you would create a dictionary of HTTP response codes and their related messages:
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
The responseMessages
variable is inferred to have type [Int: String]
. The Key
type of the dictionary is Int
, and the Value
type of the dictionary is String
.
To create a dictionary with no key-value pairs, use an empty dictionary literal ([:]
).
var emptyDict: [String: String] = [:]
Any type that conforms to the Hashable
protocol can be used as a dictionary’s Key
type, including all of Swift’s basic types. You can use your own custom types as dictionary keys by making them conform to the Hashable
protocol.
Getting and Setting Dictionary Values
The most common way to access values in a dictionary is to use a key as a subscript. Subscripting with a key takes the following form:
print(responseMessages[200])
// Prints "Optional("OK")"
Subscripting a dictionary with a key returns an optional value, because a dictionary might not hold a value for the key that you use in the subscript.
The next example uses key-based subscripting of the responseMessages
dictionary with two keys that exist in the dictionary and one that does not.
let httpResponseCodes = [200, 403, 301]
for code in httpResponseCodes {
if let message = responseMessages[code] {
print("Response \(code): \(message)")
} else {
print("Unknown response \(code)")
}
}
// Prints "Response 200: OK"
// Prints "Response 403: Access forbidden"
// Prints "Unknown response 301"
You can also update, modify, or remove keys and values from a dictionary using the key-based subscript. To add a new key-value pair, assign a value to a key that isn’t yet a part of the dictionary.
responseMessages[301] = "Moved permanently"
print(responseMessages[301])
// Prints "Optional("Moved permanently")"
Update an existing value by assigning a new value to a key that already exists in the dictionary. If you assign nil
to an existing key, the key and its associated value are removed. The following example updates the value for the 404
code to be simply “Not found” and removes the key-value pair for the 500
code entirely.
responseMessages[404] = "Not found"
responseMessages[500] = nil
print(responseMessages)
// Prints "[301: "Moved permanently", 200: "OK", 403: "Access forbidden", 404: "Not found"]"
In a mutable Dictionary
instance, you can modify in place a value that you’ve accessed through a keyed subscript. The code sample below declares a dictionary called interestingNumbers
with string keys and values that are integer arrays, then sorts each array in-place in descending order.
var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 17],
"triangular": [1, 3, 6, 10, 15, 21, 28],
"hexagonal": [1, 6, 15, 28, 45, 66, 91]]
for key in interestingNumbers.keys {
interestingNumbers[key]?.sort(by: >)
}
print(interestingNumbers["primes"]!)
// Prints "[17, 13, 11, 7, 5, 3, 2]"
Iterating Over the Contents of a Dictionary
Every dictionary is an unordered collection of key-value pairs. You can iterate over a dictionary using a for
-in
loop, decomposing each key-value pair into the elements of a tuple.
let imagePaths = ["star": "/glyphs/star.png",
"portrait": "/images/content/portrait.jpg",
"spacer": "/images/shared/spacer.gif"]
for (name, path) in imagePaths {
print("The path to '\(name)' is '\(path)'.")
}
// Prints "The path to 'star' is '/glyphs/star.png'."
// Prints "The path to 'portrait' is '/images/content/portrait.jpg'."
// Prints "The path to 'spacer' is '/images/shared/spacer.gif'."
The order of key-value pairs in a dictionary is stable between mutations but is otherwise unpredictable. If you need an ordered collection of key-value pairs and don’t need the fast key lookup that Dictionary
provides, see the KeyValuePairs
type for an alternative.
You can search a dictionary’s contents for a particular value using the contains(where:)
or firstIndex(where:)
methods supplied by default implementation. The following example checks to see if imagePaths
contains any paths in the "/glyphs"
directory:
let glyphIndex = imagePaths.firstIndex(where: { $0.value.hasPrefix("/glyphs") })
if let index = glyphIndex {
print("The '\(imagePaths[index].key)' image is a glyph.")
} else {
print("No glyphs found!")
}
// Prints "The 'star' image is a glyph.")
Note that in this example, imagePaths
is subscripted using a dictionary index. Unlike the key-based subscript, the index-based subscript returns the corresponding key-value pair as a non-optional tuple.
print(imagePaths[glyphIndex!])
// Prints "(key: "star", value: "/glyphs/star.png")"
A dictionary’s indices stay valid across additions to the dictionary as long as the dictionary has enough capacity to store the added values without allocating more buffer. When a dictionary outgrows its buffer, existing indices may be invalidated without any notification.
When you know how many new values you’re adding to a dictionary, use the init(minimumCapacity:)
initializer to allocate the correct amount of buffer.
Bridging Between Dictionary and NSDictionary
You can bridge between Dictionary
and NSDictionary
using the as
operator. For bridging to be possible, the Key
and Value
types of a dictionary must be classes, @objc
protocols, or types that bridge to Foundation types.
Bridging from Dictionary
to NSDictionary
always takes O(1) time and space. When the dictionary’s Key
and Value
types are neither classes nor @objc
protocols, any required bridging of elements occurs at the first access of each element. For this reason, the first operation that uses the contents of the dictionary may take O(n).
Bridging from NSDictionary
to Dictionary
first calls the copy(with:)
method (- copyWithZone:
in Objective-C) on the dictionary to get an immutable copy and then performs additional Swift bookkeeping work that takes O(1) time. For instances of NSDictionary
that are already immutable, copy(with:)
usually returns the same dictionary in O(1) time; otherwise, the copying performance is unspecified. The instances of NSDictionary
and Dictionary
share buffer using the same copy-on-write optimization that is used when two instances of Dictionary
share buffer.
Citizens in Swift
Members
init(
) Creates an empty dictionary.
init<S>(S, uniquingKeysWith: (Value, Value) throws -> Value
) rethrows 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.
init<S>(grouping: S, by: (S.Element) throws -> Key
) rethrows 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.
init(minimumCapacity: Int
) Creates an empty dictionary with preallocated space for at least the specified number of elements.
init<S>(uniqueKeysWithValues: S
) Creates a new dictionary from the key-value pairs in the given sequence.
typealias Element
The element type of a dictionary: a tuple containing an individual key-value pair.
Citizens in Swift
where Key:Hashable, Key:Sendable, Value:Sendable
Conformances
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Citizens in Swift
where Key:Encodable, Key:Hashable, Value:Encodable
Conformances
protocol Encodable
A type that can encode itself to an external representation.
Members
func encode(to: Encoder
) throws Encodes the contents of this dictionary into the given encoder.
Citizens in Swift
where Key:Hashable, Value:Equatable
Conformances
protocol Equatable
A type that can be compared for value equality.
Members
Features
Citizens in Swift
where Key:Hashable
Conformances
protocol Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomReflectable
A type that explicitly supplies its own mirror.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol ExpressibleByDictionaryLiteral
A type that can be initialized using a dictionary literal.
protocol Sequence
A type that provides sequential, iterated access to its elements.
Members
init(dictionaryLiteral: (Key, Value)...
) Creates a dictionary initialized with a dictionary literal.
var capacity: Int
The total number of key-value pairs that the dictionary can contain without allocating new storage.
var count: Int
The number of key-value pairs in the dictionary.
var customMirror: Mirror
A mirror that reflects the dictionary.
var debugDescription: String
A string that represents the contents of the dictionary, suitable for debugging.
var description: String
A string that represents the contents of the dictionary.
var endIndex: Dictionary<Key, Value>.Index
The dictionary’s “past the end” position—that is, the position one greater than the last valid subscript argument.
var isEmpty: Bool
A Boolean value that indicates whether the dictionary is empty.
var keys: Dictionary<Key, Value>.Keys
A collection containing just the keys of the dictionary.
var startIndex: Dictionary<Key, Value>.Index
The position of the first element in a nonempty dictionary.
var values: Dictionary<Key, Value>.Values
A collection containing just the values of the dictionary.
subscript(Key
) -> Value? Accesses the value associated with the given key for reading and writing.
subscript(Dictionary
<Key, Value>.Index) -> Dictionary<Key, Value>.Element Accesses the key-value pair at the specified position.
subscript(Key, default _: () -> Value
) -> Value Accesses the value with the given key, falling back to the given default value if the key isn’t found.
func compactMapValues<T>((Value) throws -> T?
) rethrows -> Dictionary<Key, T> Returns a new dictionary containing only the key-value pairs that have non-
nil
values as the result of transformation by the given closure.func filter((Dictionary<Key, Value>.Element) throws -> Bool
) rethrows -> [Key : Value] Returns a new dictionary containing the key-value pairs of the dictionary that satisfy the given predicate.
func formIndex(after: inout Dictionary<Key, Value>.Index
) func index(after: Dictionary<Key, Value>.Index
) -> Dictionary<Key, Value>.Index func index(forKey: Key
) -> Dictionary<Key, Value>.Index? Returns the index for the given key.
func makeIterator(
) -> Dictionary<Key, Value>.Iterator Returns an iterator over the dictionary’s key-value pairs.
func mapValues<T>((Value) throws -> T
) rethrows -> Dictionary<Key, T> Returns a new dictionary containing the keys of this dictionary with the values transformed by the given closure.
func merge([Key : Value], uniquingKeysWith: (Value, Value) throws -> Value
) rethrows Merges the given dictionary into this dictionary, using a combining closure to determine the value for any duplicate keys.
func merge<S>(S, uniquingKeysWith: (Value, Value) throws -> Value
) rethrows Merges the key-value pairs in the given sequence into the dictionary, using a combining closure to determine the value for any duplicate keys.
func merging([Key : Value], uniquingKeysWith: (Value, Value) throws -> Value
) rethrows -> [Key : Value] Creates a dictionary by merging the given dictionary into this dictionary, using a combining closure to determine the value for duplicate keys.
func merging<S>(S, uniquingKeysWith: (Value, Value) throws -> Value
) rethrows -> [Key : Value] Creates a dictionary by merging key-value pairs in a sequence into the dictionary, using a combining closure to determine the value for duplicate keys.
func popFirst(
) -> Dictionary<Key, Value>.Element? Removes and returns the first key-value pair of the dictionary if the dictionary isn’t empty.
func remove(at: Dictionary<Key, Value>.Index
) -> Dictionary<Key, Value>.Element Removes and returns the key-value pair at the specified index.
func removeAll(keepingCapacity: Bool
) Removes all key-value pairs from the dictionary.
func removeValue(forKey: Key
) -> Value? Removes the given key and its associated value from the dictionary.
func reserveCapacity(Int
) Reserves enough space to store the specified number of key-value pairs.
func updateValue(Value, forKey: Key
) -> Value? Updates the value stored in the dictionary for the given key, or adds a new key-value pair if the key does not exist.
struct Index
The position of a key-value pair in a dictionary.
struct Iterator
An iterator over the members of a
Dictionary<Key, Value>
.struct Keys
A view of a dictionary’s keys.
struct Values
A view of a dictionary’s values.
typealias SubSequence
Features
var first: Self.Element?
The first element of the collection.
var lazy: LazySequence<Self>
A sequence containing the same elements as this sequence, but on which some operations, such as
map
andfilter
, are implemented lazily.func allSatisfy((Self.Element) throws -> Bool
) rethrows -> Bool Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.
func compactMap<ElementOfResult>((Self.Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] Returns an array containing the non-
nil
results of calling the given transformation with each element of this sequence.func contains(where: (Self.Element) throws -> Bool
) rethrows -> Bool Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
func drop(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence by skipping elements while
predicate
returnstrue
and returning the remaining elements.func dropFirst(Int
) -> Self.SubSequence Returns a subsequence containing all but the given number of initial elements.
func dropLast(Int
) -> Self.SubSequence Returns a subsequence containing all but the specified number of final elements.
func elementsEqual<OtherSequence>(OtherSequence, by: (Self.Element, OtherSequence.Element) throws -> Bool
) rethrows -> Bool Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements in the same order, using the given predicate as the equivalence test.
func enumerated(
) -> EnumeratedSequence<Self> Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.
func first(where: (Self.Element) throws -> Bool
) rethrows -> Self.Element? Returns the first element of the sequence that satisfies the given predicate.
func firstIndex(where: (Self.Element) throws -> Bool
) rethrows -> Self.Index? Returns the first index in which an element of the collection satisfies the given predicate.
func flatMap<SegmentOfResult>((Self.Element) throws -> SegmentOfResult
) rethrows -> [SegmentOfResult.Element] Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.
func forEach((Self.Element) throws -> Void
) rethrows Calls the given closure on each element in the sequence in the same order as a
for
-in
loop.func formIndex(inout Self
.Index, offsetBy: Int) Offsets the given index by the specified distance.
func formIndex(inout Self
.Index, offsetBy: Int, limitedBy: Self.Index) -> Bool Offsets the given index by the specified distance, or so that it equals the given limiting index.
func lexicographicallyPrecedes<OtherSequence>(OtherSequence, by: (Self.Element, Self.Element) throws -> Bool
) rethrows -> Bool Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.
func map<T>((Self.Element) throws -> T
) rethrows -> [T] Returns an array containing the results of mapping the given closure over the sequence’s elements.
func map<T>((Self.Element) throws -> T
) rethrows -> [T] Returns an array containing the results of mapping the given closure over the sequence’s elements.
func max(by: (Self.Element, Self.Element) throws -> Bool
) rethrows -> Self.Element? Returns the maximum element in the sequence, using the given predicate as the comparison between elements.
func min(by: (Self.Element, Self.Element) throws -> Bool
) rethrows -> Self.Element? Returns the minimum element in the sequence, using the given predicate as the comparison between elements.
func prefix(Int
) -> Self.SubSequence Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.
func prefix(through: Self.Index
) -> Self.SubSequence Returns a subsequence from the start of the collection through the specified position.
func prefix(upTo: Self.Index
) -> Self.SubSequence Returns a subsequence from the start of the collection up to, but not including, the specified position.
func prefix(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence containing the initial elements until
predicate
returnsfalse
and skipping the remaining elements.func randomElement(
) -> Self.Element? Returns a random element of the collection.
func randomElement<T>(using: inout T
) -> Self.Element? Returns a random element of the collection, using the given generator as a source for randomness.
func reduce<Result>(Result, (Result, Self.Element) throws -> Result
) rethrows -> Result Returns the result of combining the elements of the sequence using the given closure.
func reduce<Result>(into: Result, (inout Result, Self.Element) throws -> ()
) rethrows -> Result Returns the result of combining the elements of the sequence using the given closure.
func reversed(
) -> [Self.Element] Returns an array containing the elements of this sequence in reverse order.
func shuffled(
) -> [Self.Element] Returns the elements of the sequence, shuffled.
func shuffled<T>(using: inout T
) -> [Self.Element] Returns the elements of the sequence, shuffled using the given generator as a source for randomness.
func sorted(by: (Self.Element, Self.Element) throws -> Bool
) rethrows -> [Self.Element] Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.
func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Self.Element) throws -> Bool
) rethrows -> [Self.SubSequence] Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.
func starts<PossiblePrefix>(with: PossiblePrefix, by: (Self.Element, PossiblePrefix.Element) throws -> Bool
) rethrows -> Bool Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.
func suffix(Int
) -> Self.SubSequence Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
func suffix(from: Self.Index
) -> Self.SubSequence Returns a subsequence from the specified position to the end of the collection.
func flatMap<ElementOfResult>((Self.Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult]
Citizens in Swift
where Key:Decodable, Key:Hashable, Value:Decodable
Conformances
protocol Decodable
A type that can decode itself from an external representation.
Members
init(from: Decoder
) throws Creates a new dictionary by decoding from the given decoder.
Citizens in Swift
where Key:Hashable, Value:Hashable
Conformances
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.
Members
func hash(into: inout Hasher
) Hashes the essential components of this value by feeding them into the given hasher.
Available in Foundation
where Key == NSAttributedString.Key, Value == Any
Members
Extension in BSON
where Key == String, Value:Primitive
Conformances
Members
Features
Extension in BSON
where Key == String, Value:Encodable
Conformances
Members
Extension in AsyncAlgorithms
where Key:Hashable
Members
init<S>(S, uniquingKeysWith: (Value, Value) async throws -> Value
) async rethrows Creates a new dictionary from the key-value pairs in the given asynchronous sequence, using a combining closure to determine the value for any duplicate keys.
init<S>(grouping: S, by: (S.Element) async throws -> Key
) async rethrows 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.
init<S>(uniqueKeysWithValues: S
) async rethrows Creates a new dictionary from the key-value pairs in the given asynchronous sequence.
Extension in SwiftSyntaxBuilder
where Key:Hashable, Key:ExpressibleByLiteralSyntax, Value:ExpressibleByLiteralSyntax
Conformances
protocol ExpressibleByLiteralSyntax
A Swift type whose value can be represented directly in source code by a Swift literal.