Thursday, January 31, 2008

Another discovery... more head scratching

I bumped into NSIndexSet (and its buddy NSMutableIndexSet) today.

It was one of those occasions where I hadn't been particularly aware of a Cocoa class (because it hadn't featured in the guides) until I tripped over it in using another class. In this case I wanted to delete a range of objects from an NSMutableArray, and therefore had need cause to research -removeObjectsAtIndexes:, and lo, there was NSIndexSet.

When I went to read the documentation on NSIndexSet, I became excited that this class was going to nicely fulfill some a requirement that's arisen for a collection of ranges. Indeed, the documentation on NSIndexSet claims that it is implemented efficiently as a set of ranges, and the API makes the right sort of noises.

On closer inspection however, it falls a little short of my needs (at least without work), and I'm left wondering if it was implemented 'just enough' for some pressing needs at Apple, but then left to wither on the vine a little. For instance, I would have liked to have been able to use fast enumeration on the indexes that are set, or better still, obtain objects representing contiguous ranges of indexes in the set. The only way to really query an NSIndexSet to get its contents (as opposed to discrete tests for particular indices), is using -getIndexes:maxCount:inIndexRange. Ouch.

Anyway, NS(Mutable)IndexSet does what you expect it to do when deployed to manage common array index tracking tasks. So, that's fine. It could just so easily have even more utility if adorned with a few more methods.

No comments: