Dictionaries in Swift

You may know that in Swift arrays to read out a value, you can call it’s index value similar to:

var someArray = ["Farhan", "Syed", "March", "19", "1996"]

someArray[0]

Instead of remembering the index value that is assigned to each value we can re-write this with a dictionary.

By dictionaries, instead of arbitrary numbers you have read and write values using a key you identify.

Let’s build a simple organized dictionary:

var someDictionary = [

"firstName":"Farhan",
 "lastName":"Syed",
 "month":"March",
 "day":"19",
 "year":"1996"

]

You can read into a dictionary like so:

someDictionary["firstName"]

You can observe with dictionaries, you build it by writing it’s key, then a colon, then it’s value.

You can read any value from the dictionary you build by knowing the key.


Posted

in

by

Comments

Leave a Reply

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