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;
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;
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.
horseshoe7 Apr 19 '12 at 20:32 2012-04-19 20:32
source share