1

I have NSMutableArray with data like: "John","Phillip","John","Andrea". I have also a String "John". I need to find that string in NSMutableArray and define which index is to show other data in other arrays. In my case index is 0 and 2.

This show only first

NSInteger index = [array indexOfObject:String];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
WildWorld
  • 517
  • 1
  • 5
  • 18

1 Answers1

4

NSArray provides a method that produces a set of indexes based on a condition that you supply:

NSIndexSet *allPositions = [array indexesOfObjectsPassingTest:
    ^BOOL (id str, NSUInteger i, BOOL *stop) {
        return [str isEqualToString:String];
    }];

This produces NSIndexSet which has all indexes of interest - in your case, it would have 0 and 2.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • How should I use this allPostions in [data objectAtIndex:allPositions] ? I need integer number to show data. – WildWorld Sep 04 '15 at 14:01
  • @WildWorld Depending on your situation, you could either [enumerate your index set using a block](http://stackoverflow.com/a/4209289/335858), or transform it into an array of `NSInteger`s using `getIndexes:maxCount:inIndexRange:` method. – Sergey Kalinichenko Sep 04 '15 at 14:08