Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an object from an NSSet

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

like image 824
node ninja Avatar asked Sep 30 '10 00:09

node ninja


People also ask

What is Nsmutableset?

An object representing a dynamic, unordered, uniquing collection, for use instead of a Set variable in cases that require reference semantics.

What is an Nsset?

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

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.


2 Answers

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.

like image 84
Matthew Flaschen Avatar answered Sep 28 '22 23:09

Matthew Flaschen


NSSet doesn't have a method objectAtIndex:

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

like image 33
No one in particular Avatar answered Sep 29 '22 00:09

No one in particular