Retrieving an Object from an NSSet

If you cannot get an object with objectAtIndex: from an NSSet, then how do you retrieve the objects?

+85
object objective-c cocoa-touch cocoa nsset
Sep 30 2018-10-09T00:
source share
7 answers

There are several options for using the kit. You can list (e.g. enumerateObjectsUsingBlock or NSFastEnumeration), call containsObject to check for membership, use anyObject to get a member (not random) or convert it to an array (in a specific order) with allObjects .

The set is suitable if you do not want duplicates, do not care about the order and want to quickly test membership.

+130
Sep 30 '10 at 0:12
source share

NSSet does not have an objectAtIndex method:

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

+28
Sep 30 '10 at 0:12
source share

you can use filterSetUsingPredicate if you have some unique identifier to select the object you need.

First create a predicate (if your unique identifier in the object is called "identifier" and this is NSString):

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

And then select the object using the predicate:

 NSObject *myChosenObject = [mySet filteredSetUsingPredicate:myPredicate].anyObject; 
+17
Aug 04 '13 at 14:20
source share

NSArray *myArray = [myNSSet allObjects];

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

replace NSUInteger with the index of your desired object.

+11
Mar 04 '13 at 6:20
source share

NSSet uses the isEqual: method (which the objects you put in this set must also override the hash method) to determine if the object is inside it.

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

 @property NSUInteger objectID; 

then you would do isEqual: how

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

and you can implement the 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 the object with this ID (and also check if it is 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 objectID that defines uniqueness (see isEqual: above) MyObject *existingObject = [mySet member: testObject]; // now you've either got it or existingObject is nil 

But yes, the only way to get something from NSSet is to consider what determines its uniqueness in the first place.

I have not tested which is faster, but I avoid using an enumeration because it can be linear, while using the member: method will be much faster. This is one reason to prefer using NSSet instead of NSArray.

+6
Apr 19 '12 at 20:32
source share

For Swift3 and iOS10:

 //your current set let mySet : NSSet //targetted index let index : Int //get object in set at index let object = mySet.allObjects[index] 
+3
Nov 28 '16 at 10:52
source share
 for (id currentElement in mySet) { // ** some actions with currentElement } 
0
Jun 20 '16 at 21:31
source share



All Articles