NSIndexSet
NSIndex
(like its mutable counterpart, NSMutable
) is a sorted collection of unique unsigned integers. Think of it like an NSRange
that supports non-contiguous series. It has wicked fast operations for finding indexes in ranges or set intersections, and comes with all of the convenience methods you’d expect in a Foundation collection class.
You’ll find NSIndex
used throughout the Foundation framework. Anytime a method gets multiple elements from a sorted collection, such as an array or a table view’s data source, you can be sure that an NSIndex
parameter will be somewhere in the mix.
If you look hard enough, you may start to find aspects of your data model that could be represented with NSIndex
. For example, AFNetworking uses an index set to represent HTTP response status codes: the user defines a set of “acceptable” codes (in the 2XX
range, by default), and the response is checked by using contains
.
The Swift standard library includes Permutation
, an often-overlooked type that dovetails nicely with NSIndex
. Permutation
wraps a collection and a sequence of indexes (sound familiar?) to allow easy iteration:
let streetscape = ["Ashmead", "Belmont", "Clifton", "Douglas", "Euclid", "Fairmont",
"Girard", "Harvard", "Irving", "Kenyon", "Lamont", "Monroe",
"Newton", "Otis", "Perry", "Quincy"]
let selected Indices = NSMutable Index Set(indexes In Range: NSRange(0...2))
selected Indices.add Index(5)
selected Indices.add Indexes In Range(NSRange(11...13))
for street in Permutation Generator(elements: streetscape, indices: selected Indices) {
print(street)
}
// Ashmead, Belmont, Clifton, Fairmont, Monroe, Newton, Otis
Here are a few more ideas to get you thinking in terms of index sets:
- Have a list of user preferences, and want to store which ones are switched on or off? Use a single
NSIndex
in combination with anSet enum
typedef
. - Filtering a list of items by a set of composable conditions? Ditch the
NSPredicate
; instead, cache the indexes of objects that fulfill each condition, and then get the union or intersection of those indexes as conditions are added and removed.
Overall, NSIndex
is a solid class. A fair bit nerdier than its collection class siblings, but it has its place. At the very least, it’s a prime example of the great functionality that you find by paying attention to what Foundation uses in its own APIs.