Note: this post begins a new series about the Swift programming language,
introduced at WWDC 2014. I’m no more experienced in Swift than anyone else
outside Apple, but I learn best by coding and talking through a problem. If
there’s a better way to approach some of these topics, get in touch on
An ordered dictionary is a useful data structure that combines attributes of
an array and a dictionary. Like an array, it stores elements in the order
they’re added, and enumeration happens in the same fixed order every time. It
also satisfies the basic idea of a dictionary: it stores key-value pairs, and
can look up values by key.
Ordered dictionaries are incredibly useful tools for all kinds of development.
To crib an example from the Swift book, an ordered dictionary might help an app
display people and their ages: you might use names for keys, ages for values,
and use the order to provide data in table cells by index path (and support
user-driven reordering, to boot).
In this article, we’ll build an ordered dictionary atop the two primitive
collection types already in Swift: an Array and a Dictionary. Let’s go!