Swift Dictionary removeValue()

The removeValue() method removes the certain key and its associated value from the dictionary.

Example


removeValue() Syntax

The syntax of the removeValue() method is:

dictionary.removeValue(forKey: key)

Here, dictionary is an object of the Dictionary class.


removeValue() Parameters

The removeValue() method takes one parameter:

  • key - The key to remove along with its associated value.

removeValue() Return Value

  • The removeValue() method returns the removed value from dictionary.

Example: Swift removeValue()

Output

Before: ["Two": 2, "one": 1, "Three": 3]
After: ["Two": 2, "one": 1]

In the above example, we have created a dictionary named numbers with three key-value pairs.

Here, we have used the removeValue() method to remove the key named "Three" and its associated value 3.