Add Item to a Dictionary in Swift

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.


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!