Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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.


// 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