
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an Empty Dictionary in Swift
A dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers of any data type like string, integer, etc., that are connected with each value. In Swift, we are allowed to create an empty dictionary. An empty dictionary is a dictionary which does not contain any element or we can say that its size is zero.
Syntax
let myDicti = [Int:Int]() Or let myDicti:[Int:Int]= [:] Or let myDicti:[String:Int]= [:]
So to create an empty dictionary you can use any of the given syntaxes. Here it is not necessary that the data type of keys and values should be the same it can be different.
Example
In the following Swift program, we will create an empty dictionary in which the data type of both key and value is the same. And then we will find the size of the dictionary and display it in the output.
import Foundation import Glibc // Creating an empty dictionary var myDict = [String:String]() // Counting the size of the dictionary var size = myDict.count // Displaying set print("The size of the dictionary is:", size)
Output
The size of the dictionary is: 0
Conclusion
So this is how we can create an empty dictionary. Just like other collections like an empty array, empty set, etc., an empty dictionary also does not contain any element in it and we created an empty dictionary so that we can store key-value pairs later on in the program.