Create Swift Arrays and Dictionary

Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.

var shoppingList = ["bread", "eggs", "water", "jelly"]

print(shoppingList[1])

var vehicles = [

    "Toyota": "Cars",

    "Harley": "Bike",

]

vehicles["Honda"] = "Coupe"

print(vehicles[“Harley"]!)

If you have no issues with data type of arrays  you can simply create array like this

shoppingList = []

vehicles = [:]

and if you want to specify the datatype.

let emptyArray = [String]()

let emptyDictionary = [String: Float]()

Leave a Reply

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