1

I have two NSArray variables, each one of then gets a similar NSDictionary inside.

For a given row (indexPath.row):

 NSArray* array1 = ...;
 NSDictionary* item = [array1 objectAtIndex:indexPath.row];
 int myid = [[item valueForKey:@"id"] intValue];
 // id = 5

Now I need to find the element with id = 5 in array2, but because I'm a c# developer, I think it is a little weird to use a for to do that.

Is there any alternatives to a for?

lolol
  • 4,287
  • 3
  • 35
  • 54
  • How a `for()` loop is even related to this problem? What you have so far is just fine. Except that you should really not use `id` as a variable name in Objective-C as it's a type name for a generic object type. –  Oct 17 '12 at 17:30
  • Maybe I'm not being clear. I have to find the id 5 in the second array. – lolol Oct 17 '12 at 17:32
  • So you want to find the dictionary in `array2` of which the value corresponding to the `id` key is also 5? –  Oct 17 '12 at 17:34
  • Yep! My bad if I'm not clear. English is not my first language. – lolol Oct 17 '12 at 17:34
  • no problem, neither is it for me. Well, a for loop is not a bad solution in this case, why do you think it's weird? –  Oct 17 '12 at 17:36
  • I think it is weird because in c# we do that in a one line linq query (I know that a linq query is, essentially, a for loop). Is there any alternatives in iOS? – lolol Oct 17 '12 at 17:37
  • What type does [item valueForKey:@"id"] have? It is important so I can give you the solution. Is it NSString? – Daniel Oct 17 '12 at 17:38
  • @Daniel if it responds to `intValue` it's presumably an NSString or NSNumber. –  Oct 17 '12 at 17:38
  • @lolol however, see Martin R's solution, it's also a loop (internally), but it's rather elegant. –  Oct 17 '12 at 17:39
  • 1
    Can not be int. This might help: http://stackoverflow.com/questions/2697749/testing-if-nsmutablearray-contains-a-string-object – Daniel Oct 17 '12 at 17:42
  • No, it does not. I have a dictionary inside each position of the array, and what I need to compare is a particular element of the dictionary. – lolol Oct 17 '12 at 17:46
  • @H2CO3 yeh, I'm gonna use it, thank you! – lolol Oct 17 '12 at 18:25

1 Answers1

9

NSArray has a method indexOfObjectPassingTest which you can use:

NSArray *array2 = ...;
int searchId = ...;
NSUInteger index2 = [array2 indexOfObjectPassingTest:^BOOL(NSDictionary *item, NSUInteger idx, BOOL *stop) {
    BOOL found = [[item objectForKey:@"id"] intValue] == searchId;
    return found;
}];

if (index2 != NSNotFound) {
    // First matching item at index2.
} else {
    // No matching item found.
}

This returns the first matching index. If you need all matching indexes, use indexesOfObjectsPassingTest.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • It works, nice code. When the value is not found it gives me the int max value. – lolol Oct 17 '12 at 18:07
  • 1
    @lolol: Yes, it returns `NSNotFound` if item was found. `NSNotFound` is `LONG_MAX`, which is the same as `INT_MAX` on a 32-bit architecture. I will update the answer accordingly. – Martin R Oct 17 '12 at 18:13