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 trialJuan Bermudez
2,180 PointsHow is an Array different from a Dictionary?
In this video, Amit added United States to the dictionary countries["US"] = "United States"
However, in an Array things can also be added to it in a similar way,
What is the difference between both?
1 Answer
thecompanion
11,729 PointsAn array is essentially an ordered list of values, whereas a dictionary is an unordered list of key-value pairings. Together, arrays and dictionaries are 'collection types' and store data that we can access later.
For an array, you might think of a shopping list. ex. [milk, eggs, bread, bananas, coffee]. Each value is in a specific order (found at specific location within the array, i.e. 0, 1, 2....etc).
A dictionary might be something like a list of countries and their corresponding codes, like Amit used in the video. ex. "US: United States" is a key: value pair. There could be hundreds of pairings in your dictionary, but they need not be in any particular order. You just have to tell Swift the key you're searching for so it can return the correct value pair. In an array, you'd simply tell Swift to "find the value at the 0 position in the array," i.e. let arraySample = shoppingList[0]; this is the "milk" string. Order matters in arrays, not so for dictionaries.
see below for more info: https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html