I'm a bit new to Cocoa and I was reading about NSIndexSet
. Why is it necessary? It seems to me that NSIndexSet
is nothing but a NSSet
of integers, is that right? What's the purpose of creating a separate collection alltogether?
Asked
Active
Viewed 1,491 times
7

S.L. Barth is on codidact.com
- 8,198
- 71
- 51
- 66

Tony
- 36,591
- 10
- 48
- 83
1 Answers
12
There are a couple reasons:
NSIndexSet
stores unsigned integer primitive types, whereasNSSet
stores objects.NSIndexSet
is optimized for storing unsigned integers, specifically a set of integers into another data structure like anNSArray
.

mipadi
- 398,885
- 90
- 523
- 479
-
1To elaborate on #2, NSIndexSet lets you iterate through the indexes in order or in reverse order. An NSSet is unordered, and an NSOrderedSet is manually ordered (meaning you can break the order by inserting an object at the wrong position). – Peter Hosey Dec 19 '11 at 04:29
-
Got it. Another thing I just noticed. NSIndexSet actually uses NSRange to store consecutive indexes, indeed very optimized for storing index value into other collections. – Tony Dec 19 '11 at 07:37
-
Operations over `NSSet` are O(1) (hashing), while most of the `NSIndexSet` operations are O(N) (looping). – alex-i Aug 22 '16 at 07:57