-1
// 1 - Find the matching item index
NSIndexSet* indexes = [[self.orderItems allKeys] indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    IODItem* key = obj;
    return [searchItem.name isEqualToString:key.name] && 
    searchItem.price == key.price;
}];
// 2 - Return first matching item
if ([indexes count] >= 1) {
    IODItem* key = [[self.orderItems allKeys] objectAtIndex:[indexes firstIndex]];
    return key;
}

In this code I am checking through all the keys in the dictionary and comparing that with a search item.

When does this dictionary have multiple keys that have the same search item?

What I know for keys is that they are unique but this code is maintaining an index set for all the occurrences of the key that match to a particular search item.

1 Answers1

0

The test is not comparing the keys. The keys are (apparently) instances of IODItem. The test is comparing two specific properties, name and price, of those items. Depending on how -isEqual: and -hash are defined for IODItem, there may well be more than one key with the same name and price and thus more than one key which passes the test. Therefore, there may be more than one index in indexes.

But the code is not actually checking for there being more than one index. It's checking for there being at least one (i.e. more than zero).

Ken Thomases
  • 83,395
  • 7
  • 101
  • 140