These two collection types are rarely used by me as I'm typically using their counter parts, NSArray
, NSDictionary
and the equivalent mutables. I have a feeling I'm missing out on a collection which can act much faster than the other two in certain situations. When is the ideal time to use these and how?
An NSSet is much like an NSArray, the only difference is that the objects it holds are not ordered. So when you retrieve them they may come back in any random order, based on how easy it is for the system to retrieve them.
An object representing a static, unordered collection of unique objects.
NSSet
is used when you frequently wish to test for membership of an object, but don't care about the ordering and don't need the collection to contain duplicate objects. An NSSet
doesn't store its objects in any particular order; rather, the class is expressly designed to efficiently test whether an object exists in the collection.
The same goes for NSCountedSet
, although NSCountedSet
also keeps a count of duplicated objects.
So in a nutshell: use a set when you are concerned about testing for membership, rather than just keeping a collection of objects (some of which may be duplicated, as decided by an isEqualTo
method of some sort).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With