Create an Empty Dictionary in Swift

There are a few ways to create an empty dictionary in Swift. Below are a few code snippets in Swift that demonstrate how to do it.

Creating an Empty Dictionary

The below code snippet will create an empty Dictionary that has a key and a value of a String data type.

let myDictionary = [String:String]()

Another way to create an empty dictionary

let myDictionary2:[String:String] = [:]

Keys in a dictionary can also be of type Int

let myDictionary3 = [Int:String]()

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.

Leave a Reply

Your email address will not be published.