Getting an object from an NSSet

node ninja picture node ninja · Sep 30, 2010 · Viewed 92.8k times · Source

If you can't get an object with objectAtIndex: from an NSSet then how do you retrieve objects?

Answer

Matthew Flaschen picture Matthew Flaschen · Sep 30, 2010

There are several use cases for a set. You could enumerate through (e.g. with enumerateObjectsUsingBlock or NSFastEnumeration), call containsObject to test for membership, use anyObject to get a member (not random), or convert it to an array (in no particular order) with allObjects.

A set is appropriate when you don't want duplicates, don't care about order, and want fast membership testing.