- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Remove all the elements from Dictionary
In a dictionary, to remove all the elements from the dictionary Swift provides an inbuilt function named as removeAll() function. It will remove all the key-value pairs from the specified dictionary.
Syntax
dict.removeAll()
Here, dict is the dictionary. The removeAll() function does not take any parameter and removes all the key-value pairs present in the specified dictionary.
Algorithm
Step 1 − Create a dictionary with key-value pairs.
Step 2 − Print the original dictionary.
Step 3 − Now remove all the key-value pairs from the dictionary using removeAll() function.
Step 4 − Print the output.
Example
In the following Swift program, we are going to remove all the elements from the dictionary. So for that, we will create a dictionary with key-value pairs. Then we use the removeAll() function to remove all the elements from the given dictionary.
import Foundation import Glibc var myCityRank:[String:Int] = ["Delhi": 31, "Goa": 3, "Pune": 1, "Jaipur": 2, "Kolkata": 87] print("Original Dictionary:", myCityRank) myCityRank.removeAll() print("Modified Dictionary:", myCityRank)
Output
Original Dictionary: ["Goa": 3, "Kolkata": 87, "Pune": 1, "Delhi": 31, "Jaipur": 2] Modified Dictionary: [:]
Conclusion
So this is how we can remove all the elements from the dictionary using the removeAll() function. The removeAll() method modifies the original dictionary and removes all the pairs from the specified dictionary. Or we can say that the remove all function returns an empty dictionary.