1

Reading up the documentation about NSSet it says:

You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration.

Thats exactly what I need, as performance is very important to me.

So I have overriden the hash in the class that will eventually be populated in the NSSet.

- (NSUInteger)hash
{
    return [[self recordDate] hash];
}

In other words recordDate is always unique in my case and I expect to have unique hashes here.

According to the documentation it seems the only way I could lookup an object in the NSSet/NSMutableSet would be through predicates.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordDate == %@", recordDate];
NSSet *objectsWithDesiredWeek = [mySet filteredSetUsingPredicate:predicate];

I am not sure, if I am not overlooking something. Is this the most efficient way to lookup an object in the NSSet? Is there not something like Key/Value?

Houman
  • 64,245
  • 87
  • 278
  • 460
  • 1
    If you want an associative container, you can use `NSDictionary`. Honestly, I don't believe that looking up an item in a set via a Predicate is efficient. Not only is the predicate some sort of "program" that needs to be interpreted (akin regex), but the time complexity is also very likely O(n). That is, the predicate needs to be applied on all objects until one is found. – CouchDeveloper Oct 28 '13 at 23:57
  • 1
    You might want to ask about your problem rather than your imagined part solution. – Wain Oct 29 '13 at 00:02
  • You could also use objectsPassingTest: to find what you want. I don't know if that's more efficient or not. – rdelmar Oct 29 '13 at 00:11

1 Answers1

0

I guess I don't understand the question. NSSet has the built-in method containsObject. Why does that not meet your need?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • YEs Duncan, I think this is the solution. I found later this article that helped me to understand it better: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/Collections/Collections.html But I also need to implement both `hash` and `isEqual` to have `containsObject` work efficiently, so I understood. – Houman Oct 29 '13 at 01:45