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 Swift 2.0 Collections and Control Flow Dictionaries Dealing With Non-Existent Data

Raphael Reiter
Raphael Reiter
6,820 Points

Arrays Vs Dictionaries

Hi

It seems like the syntax for arrays and dictionaries are the same. How do we differentiate the two?

Best

R

1 Answer

Hi Raphael,

The key difference between an array and a dictionary is order. An array will preserve the order of its elements - a dictionary does not need to.

An array is a store of objects in a row with an index referring to each elements' position - [0], [1], [2] etc. Whatever you store at [1] will remain at [1] and that will always be 'after' [0] and 'before' [2]. You can access the first, last, next and previous elements in an array with consistent results - their order is preserved.

A dictionary stores key/value pairs. Each key is unique and is used to access the corresponding value - where those key/value pairs are in relation to each other is both irrelevant an uncontrolled - don't try to access the 'next' element of a dictionary as you could get anything returned. There are plenty of dictionary examples in the preceding videos - it doesn't need me to make one up to illustrate the point!

I hope that helps.

Steve.