Dictionaries

Declare:

var months = [Int: String]()
//The key is an integer, the value is a string
//the value can also be an array
var months = [Int: [String]]()

Add key value pair to it:
months[2] = "February"
//If the value were an array:
months[2] = ["February", "Feb"]

Access values:
print(months[2]?[0])
//getting the value of an array
//will get "February"





Notes:
-Another collection, apart from Arrays and Sets
- Has key-value pairs, where you can look up a value based on a key.


Comments

Popular posts from this blog

Setting up a playground

Go to another page