Swift Dictionaries

This tutorial belongs to the Swift series

We use dictionaries to create a collection of key-value pairs.

Here is how to create a dictionary with 1 key-value pairs, where the key is a String and the value is an Int:
var dict = ["Roger&#…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This tutorial belongs to the Swift series

We use dictionaries to create a collection of key-value pairs.

Here is how to create a dictionary with 1 key-value pairs, where the key is a String and the value is an Int:

var dict = ["Roger": 8, "Syd": 7]

In this case the type is inferred. You can also explicitly set the type at declaration time:

var dict: [String: Int] = ["Roger": 8, "Syd": 7]

In this example we create an empty dictionary of Int keys and String values:

var dict = [String: Int]()

//or

var dict: [String: Int] = [:]

You can access the value assigned to a key using this syntax:

var dict = ["Roger": 8, "Syd": 7]

dict["Roger"] //8
dict["Syd"] //7

You can change the value assigned to a key in this way:

dict["Roger"] = 9

A dictionary must be declared as var to be modified. If it’s declared with let, you cannot modify it by adding or removing elements.

Use the same syntax to add a new key/value pair:

dict["Tina"] = 4

To remove a key/value paid, assign the value to nil:

dict["Tina"] = nil

Or call the removeValue(forKey:) method:

dict.removeValue(forKey: "Tina")

To get the number of items in the dictionary, use the count property:

var dict = ["Roger": 8, "Syd": 7]
dict.count //2

If a dictionary is empty, its isEmpty property is true.

var dict = [String: Int]()
dict.isEmpty //true

There are a lot of methods related to dictionaries, but those are the basic ones.

Dictionaries are passed by value, which means if you pass an array to a function, or return it from a function, the dictionary is copied.

Dictionaries are collections, and they can be iterated over in loops.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-05T05:00:00+00:00) Swift Dictionaries. Retrieved from https://www.scien.cx/2021/06/05/swift-dictionaries/

MLA
" » Swift Dictionaries." flaviocopes.com | Sciencx - Saturday June 5, 2021, https://www.scien.cx/2021/06/05/swift-dictionaries/
HARVARD
flaviocopes.com | Sciencx Saturday June 5, 2021 » Swift Dictionaries., viewed ,<https://www.scien.cx/2021/06/05/swift-dictionaries/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift Dictionaries. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/05/swift-dictionaries/
CHICAGO
" » Swift Dictionaries." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/05/swift-dictionaries/
IEEE
" » Swift Dictionaries." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/05/swift-dictionaries/. [Accessed: ]
rf:citation
» Swift Dictionaries | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/05/swift-dictionaries/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.