Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSet and NSCountedSet, when is the best time to use them?

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?

like image 956
Coocoo4Cocoa Avatar asked May 22 '09 13:05

Coocoo4Cocoa


People also ask

What's a difference between NSArray and NSSet?

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.

What is NSSet in Objective C?

An object representing a static, unordered collection of unique objects.


1 Answers

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).

like image 75
mipadi Avatar answered Sep 28 '22 17:09

mipadi