iOS Build a Vending Machine App in Swift Loading Data From a Resource Type Casting

Huseyn Guliyev
Huseyn Guliyev
2,603 Points

Arrays and Dictionaries

I know this is not the right place to ask, but I am going to do it anyway. What is the difference between an array and a dictionary?

1 Answer

Jhoan Arango
MOD
Jhoan Arango
Treehouse Moderator 13,603 Points

This is the right place to ask...

An array stores values of the same type in order. A dictionary also stores values but with a key on them, this is called a key value pair, and they are not ordered in a particular way.

Examples:

// Array

let numbers: [Int] = [1,2,3,4,5,6,7,8,9]

// To get a value from the array,
// we use the index where the value is at.
// If we want the first value we use "0"

let one = numbers[0]
print(one) // This will print the number 1

// Dictionary

let numbersDictionary: [String: Int] = ["one" : 1, "two" : 2, "three": 3]

// To get a value from the dictionary,
// we use the key for the value.

let three = numbersDictionary["three"]!
print(three) // this will print the number 3