Dictionary

Let’s create a Dictionary with two elements first. var myDictionary = [“first_name”: “Sergey”, “last_name”: “Kargopolov”] Now we can remove all elements from a Dictionary. myDictionary.removeAll() print(myDictionary) For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.

Read More Remove All Items From a Dictionary

First, let’s create a Dictionary with two elements. var myDictionary = [“first_name”: “Sergey”, “last_name”: “Kargopolov”] print(myDictionary) Now we can add a new key with a value. myDictionary[“user_id”] = “f5h7ru0tJurY8f7g5s6fd” We should now have 3 key-value pairs printed print(myDictionary) For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.

Read More Add Item to a Dictionary in Swift

In this short tutorial, you will learn how to remove a single item from a dictionary in Swift as well as how to remove all items from a dictionary. Create a New Dictionary Let’s say you have created the following dictionary with a few key-value pairs in it. var myDictionary = [“firstName”: “Sergey”, “lastName”: “Kargopolov”,…

Read More Remove an Item From a Dictionary in Swift

This page contains different code snippets on how to work with Dictionary in Swift. Create Empty Dictionary // Create an empty dictionary let myDictionary = [String:String]() // Another way to create an empty dictionary let myDictionary2:[String:String] = [:] // Keys in dictionary can also be of type Int let myDictionary3 = [Int:String]()   Add Item…

Read More Working with Dictionary in Swift. Code Examples.

In this very short Swift tutorial, you will learn how to loop or iterate over a dictionary in Swift and print out key/value pairs. Loop Through Dictionary // Create a Dictionary with two elements var myDictionary = [“first_name”: “Sergey”, “last_name”: “Kargopolov”] // Loop through dictionary and print key/value pair for (key,value) in myDictionary { print(“\(key)…

Read More Loop Through Dictionary in Swift