Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
Jhoan Arango
14,575 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