iOS, Objective-C, Swift, Design and Whatever Comes in Mind
‹ Back to the Blog

Overlooked API of the Day: NSOrderedSet

As you know, an Array stores elements while preserving the order of their addition. You also may have learned that Sets are perfect for storing a collection of data where every element is unique. Because of its implementation it is also easy to perform boolean operations on two Sets (like computing the intersection or difference of two Sets). However, Sets will not preserve the order of their elements. What is more, they gain their efficiency only from not remembering information about the order.

This is where NSOrderedSet comes into play. According to Apples documentation, it acts as a Set (containing an element only once) whilst preserving the order of their additions.

Its counterpart NSMutableOrderedSet is the mutable addition to the datatype.

"Overlooked API of the Day: NSOrderedSet".