Swift Dictionary removeAll()

The removeAll() method removes all key-value pairs from the dictionary.

Example

// create a dictionary with two elements
var information = ["Charlie": 54, "Harvey": 34]

// remove all elements using removeAll() information.removeAll()
// print updated dictionary print(information) // Output: [:]

removeAll() Syntax

The syntax of the removeAll() method is:

dictionary.removeAll()

Here, dictionary is an object of the Dictionary class.


removeAll() Parameters

The removeAll() method doesn't take any parameter.


removeAll() Return Value

  • The removeAll() method doesn't return any value. It only removes key-value pairs from the dictionary.

Example: Swift removeAll()

// create a dictionary with three elements
var numbers = ["one": 1, "Two": 2, "Three": 3]

print("Before:", numbers)

// remove all key-value pairs using removeAll() numbers.removeAll()
// print updated dictionary print("After:", numbers)

Output

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

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

Here, we have used the removeAll() method to remove all key-value pairs from numbers.

Did you find this article helpful?