Difference between revisions of "Dictionaries"
m (Chukwuemeka-tinashe moved page W2273 Dictionaries to Dictionaries without leaving a redirect: Improved navigation) |
(→Loops) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
== | == Introduction == | ||
A dictionary is a data type that functions similarly to an array in that it holds multiple keyed values. A key feature of a dictionary is the ability to use nearly any data type as the key. For example, a dictionary can be used to store a collection of student GPAs keyed by their names: | |||
<syntaxhighlight lang="swift"> | |||
[ | |||
"Potter": 4.0, | |||
"Bob": 1.2, | |||
"Merlin": 2.5, | |||
"Tim": 3.0 | |||
] | |||
</syntaxhighlight> | |||
== Order Is NOT Preserved == | |||
It's important to note that the order of keys and values will not necessarily remain the same. This can be seen by running the examples below, in which the output will not match up to the order in the definition. | |||
== Defining Dictionaries In Swift == | |||
Defining a dictionary in Swift has a similar syntax to defining arrays. | |||
=== Empty Dictionary === | |||
To initialize an empty dictionary, the syntax is an opening bracket '''[''', followed by the data type for the key (e.g. '''String'''), followed by a colon ''':''', then the data type for the values (e.g. '''Double'''), then a closing bracket ''']''' and a pair of parenthesis: | |||
<syntaxhighlight lang="swift"> | |||
var dict = [String:Double]() | |||
</syntaxhighlight> | |||
=== With Pre-defined Values === | |||
Setting up a dictionary with pre-defined values is also similar to the same definition with an array: | |||
== | <syntaxhighlight lang="swift"> | ||
var dict = [ | |||
"Potter": 4.0, | |||
"Bob": 1.2, | |||
"Merlin": 2.5, | |||
"Tim": 3.0 | |||
] | |||
</syntaxhighlight> | |||
== | === Types === | ||
When you would like to accept a dictionary as a parameter to a function, you need to specify the type of key and value: | |||
<syntaxhighlight lang="swift"> | |||
func prettyPrint(dict: [String:Double]) { | |||
... | |||
} | |||
</syntaxhighlight> | |||
== Keys == | |||
As previously mentioned, a dictionary can have keys of nearly any data type. Working with indexes is also extremely similar to working with arrays. For example, this would retrieve the value for the key "Potter": | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
/ | dict["Potter"] | ||
</syntaxhighlight> | |||
Similarly, this would either create or update the value keyed by "Potter" to a value of 3.5: | |||
<syntaxhighlight lang="swift"> | |||
dict["Potter"] = 3.5 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Loops == | ||
Also similar to arrays is how the values of a dictionary can be iterated through by using a '''for''' loop. Here is an example of using a '''for''' loop to create a ''prettyPrint'' function: | |||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=1 | |exerciseID=1 | ||
| | |height=150 | ||
| | |language=swift | ||
|initialCode= | |initialCode= | ||
func prettyPrint(dict: [String:Double]) { | |||
print("[") | |||
for (key, value) in dict { | |||
print(" \"\(key)\": \(value)") | |||
} | |||
print( " | print("]") | ||
} | |||
prettyPrint(dict: [ | |||
"Potter": 4.0, | |||
"Bob": 1.2, | |||
"Merlin": 2.5, | |||
"Tim": 3.0 | |||
]) | |||
}} | }} | ||
Notice that the for loop defines two variables for each iteration, '''key''' and '''value'''. If you try to only retrieve the value, as would ordinarily be done when iterating through an array, a tuple will be returned instead of the expected value: | |||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=2 | |exerciseID=2 | ||
| | |height=150 | ||
| | |language=swift | ||
|initialCode= | |initialCode= | ||
func prettyPrint(dict: [String:Double]) { | |||
print("[") | |||
print( " | |||
for value in dict { | |||
print( " | print(" \(value)") | ||
} | |||
print("]") | |||
print( " | } | ||
prettyPrint(dict: [ | |||
"Potter": 4.0, | |||
"Bob": 1.2, | |||
"Merlin": 2.5, | |||
"Tim": 3.0 | |||
]) | |||
}} | }} | ||
Latest revision as of 22:44, 23 January 2023
Introduction[edit]
A dictionary is a data type that functions similarly to an array in that it holds multiple keyed values. A key feature of a dictionary is the ability to use nearly any data type as the key. For example, a dictionary can be used to store a collection of student GPAs keyed by their names:
[
"Potter": 4.0,
"Bob": 1.2,
"Merlin": 2.5,
"Tim": 3.0
]
Order Is NOT Preserved[edit]
It's important to note that the order of keys and values will not necessarily remain the same. This can be seen by running the examples below, in which the output will not match up to the order in the definition.
Defining Dictionaries In Swift[edit]
Defining a dictionary in Swift has a similar syntax to defining arrays.
Empty Dictionary[edit]
To initialize an empty dictionary, the syntax is an opening bracket [, followed by the data type for the key (e.g. String), followed by a colon :, then the data type for the values (e.g. Double), then a closing bracket ] and a pair of parenthesis:
var dict = [String:Double]()
With Pre-defined Values[edit]
Setting up a dictionary with pre-defined values is also similar to the same definition with an array:
var dict = [
"Potter": 4.0,
"Bob": 1.2,
"Merlin": 2.5,
"Tim": 3.0
]
Types[edit]
When you would like to accept a dictionary as a parameter to a function, you need to specify the type of key and value:
func prettyPrint(dict: [String:Double]) {
...
}
Keys[edit]
As previously mentioned, a dictionary can have keys of nearly any data type. Working with indexes is also extremely similar to working with arrays. For example, this would retrieve the value for the key "Potter":
dict["Potter"]
Similarly, this would either create or update the value keyed by "Potter" to a value of 3.5:
dict["Potter"] = 3.5
Loops[edit]
Also similar to arrays is how the values of a dictionary can be iterated through by using a for loop. Here is an example of using a for loop to create a prettyPrint function:
Notice that the for loop defines two variables for each iteration, key and value. If you try to only retrieve the value, as would ordinarily be done when iterating through an array, a tuple will be returned instead of the expected value: