Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Dictionary removeAll()
In this tutorial, we will learn about the Swift Dictionary removeAll() method with the help of examples.
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?