Instance Methodswift 6.0.1Swift
removeValue(forKey:)
Removes the given key and its associated value from the dictionary.
@discardableResult mutating func removeValue(forKey key: Key) -> Value?
Parameters
- key
The key to remove along with its associated value.
Returns
The value that was removed, or nil
if the key was not present in the dictionary.
If the key is found in the dictionary, this method returns the key’s associated value. On removal, this method invalidates all indices with respect to the dictionary.
var hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
if let value = hues.removeValue(forKey: "Coral") {
print("The value \(value) was removed.")
}
// Prints "The value 16 was removed."
If the key isn’t found in the dictionary, removeValue(forKey:)
returns nil
.
if let value = hues.removeValue(forKey: "Cerise") {
print("The value \(value) was removed.")
} else {
print("No value found for that key.")
}
// Prints "No value found for that key.""
Other members in extension
Types
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.
Typealiases
Type members
init(dictionaryLiteral: (Key, Value)...
) Creates a dictionary initialized with a dictionary literal.
Instance members
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: @autoclosure () -> 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 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.