Are predicates the most efficient way to lookup an object in NSSet?

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?


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

链接地址: http://www.djcxy.com/p/85158.html

上一篇: 清除NSUserDefaults

下一篇: 谓词是在NSSet中查找对象的最有效方法吗?