Skip to content
Swift

Dictionary API Reference

Swift's Dictionary<K, V> — a collection of unique keys mapped to values, with O(1) average lookup.

By EZ4Code Team

Dictionary

A collection whose elements are key-value pairs. A value type bridged to NSDictionary.

Dictionary subscript [Key] -> Value?

Accesses the value associated with the given key, returning nil if the key is not present.

Returns: Value?

Dictionary.count -> Int

The number of key-value pairs in the dictionary.

Returns: Int

Dictionary.keys -> Dictionary.Keys

A collection containing just the keys of the dictionary.

Returns: Dictionary.Keys

Dictionary.values -> Dictionary.Values

A collection containing just the values of the dictionary.

Returns: Dictionary.Values

Dictionary.updateValue(_ value: Value, forKey key: Key) -> Value?

Updates the value for the given key, returning the old value (or nil if key was new).

Returns: Value?

Dictionary.removeValue(forKey: Key) -> Value?

Removes the given key and its associated value, returning the removed value (or nil).

Returns: Value?

Dictionary.forEach(_ body: ((key: Key, value: Value)) throws -> Void) rethrows

Calls the given closure on each key-value pair in the dictionary.

Returns: Void

More Swift API References