Getting an object from an NSSet

Objective CCocoaCocoa TouchObjectNsset

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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.

Solution 2 - Objective C

NSSet doesn't have a method objectAtIndex:

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

Solution 3 - Objective C

it is possible to use filteredSetUsingPredicate if you have some kind of unique identifier to select the object you need.

First create the predicate (assuming your unique id in the object is called "identifier" and it is an NSString):

NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"identifier == %@", identifier];

And then choose the object using the predicate:

NSObject *myChosenObject = [mySet filteredSetUsingPredicate:myPredicate].anyObject;

Solution 4 - Objective C

NSArray *myArray = [myNSSet allObjects];

MyObject *object = [myArray objectAtIndex:(NSUInteger *)]

replace NSUInteger with the index of your desired object.

Solution 5 - Objective C

For Swift3 & iOS10 :

//your current set
let mySet : NSSet
//targetted index
let index : Int
//get object in set at index
let object = mySet.allObjects[index]

Solution 6 - Objective C

NSSet uses the method isEqual: (which the objects you put into that set must override, in addition, the hash method) to determine if an object is inside of it.

So, for example if you have a data model that defines its uniqueness by an id value (say the property is:

@property NSUInteger objectID;

then you'd implement isEqual: as

- (BOOL)isEqual:(id)object
{
  return (self.objectID == [object objectID]);
}

and you could implement hash:

- (NSUInteger)hash
{
 return self.objectID;  // to be honest, I just do what Apple tells me to here
                       // because I've forgotten how Sets are implemented under the hood
}

Then, you can get an object with that ID (as well as check for whether it's in the NSSet) with:

MyObject *testObject = [[MyObject alloc] init];
testObject.objectID = 5; // for example.  

// I presume your object has more properties which you don't need to set here 
// because it's objectID that defines uniqueness (see isEqual: above)

MyObject *existingObject = [mySet member: testObject];

// now you've either got it or existingObject is nil

But yeah, the only way to get something out of a NSSet is by considering that which defines its uniqueness in the first place.

I haven't tested what's faster, but I avoid using enumeration because that might be linear whereas using the member: method would be much faster. That's one of the reasons to prefer the use of NSSet instead of NSArray.

Solution 7 - Objective C

for (id currentElement in mySet)
{
    // ** some actions with currentElement 
}

Solution 8 - Objective C

Most of the time you don't care about getting one particular object from a set. You care about testing to see if a set contains an object. That's what sets are good for. When you want to see if an object is in a collection sets are much faster than arrays.

If you don't care about which object you get, use -anyObject which just gives you one object from the set, like putting your hand in a bag and grabbing something.

Dog *aDog = [dogs anyObject]; // dogs is an NSSet of Dog objects

If you care about what object you get, use -member which gives you back the object, or nil if it's not in the set. You need to already have the object before you call it.

Dog *spot = [Dog dogWithName:@"Spot"];
// ...
Dog *aDog = [dogs member:spot]; // Returns the same object as above

Here's some code you can run in Xcode to understand more

NSString *one = @"One";
NSString *two = @"Two";
NSString *three = @"Three";

NSSet *set = [NSSet setWithObjects:one, two, three, nil];

// Can't use Objective-C literals to create a set.
// Incompatible pointer types initializing 'NSSet *' with an expression of type 'NSArray *'
//	NSSet *set = @[one, two, three];

NSLog(@"Set: %@", set);
// Prints looking just like an array but is actually not in any order
//Set: {(
//	   One,
//	   Two,
//	   Three
//	   )}

// Get a random object
NSString *random = [set anyObject];
NSLog(@"Random: %@", random); // Random: One

// Iterate through objects. Again, although it prints in order, the order is a lie
for (NSString *aString in set) {
	NSLog(@"A String: %@", aString);
}

// Get an array from the set
NSArray *array = [set allObjects];
NSLog(@"Array: %@", array);

// Check for an object
if ([set containsObject:two]) {
	NSLog(@"Set contains two");
}

// Check whether a set contains an object and return that object if it does (nil if not)
NSString *aTwo = [set member:two];
if (aTwo) {
	NSLog(@"Set contains: %@", aTwo);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnode ninjaView Question on Stackoverflow
Solution 1 - Objective CMatthew FlaschenView Answer on Stackoverflow
Solution 2 - Objective CNo one in particularView Answer on Stackoverflow
Solution 3 - Objective CVitaliiView Answer on Stackoverflow
Solution 4 - Objective CkrischuView Answer on Stackoverflow
Solution 5 - Objective CKevin ABRIOUXView Answer on Stackoverflow
Solution 6 - Objective Chorseshoe7View Answer on Stackoverflow
Solution 7 - Objective CSavchenko DmitryView Answer on Stackoverflow
Solution 8 - Objective Cnevan kingView Answer on Stackoverflow