views:

139

answers:

2

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

+6  A: 

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.

Matthew Flaschen
I think you're missing a link to https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html#//apple_ref/doc/uid/20000143-SW21.
Robot K
@Robot, thanks, fixed.
Matthew Flaschen
You can also look up a known object by a potential duplicate by sending the set a `member:` message. If it returns `nil`, the set does not contain an object equal to the one you passed; if it returns an object pointer, then the pointer it returns is to the object already in the set. The objects in the set must implement `hash` and `isEqual:` for this to be useful.
Peter Hosey
+1  A: 

NSSet doesn't have a method objectAtIndex:

Try calling allObjects which returns an NSArray of all the objects.

No one in particular