WildWorld
WildWorld

Reputation: 517

Find all indexes of a string inside NSArray

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];

Upvotes: 1

Views: 172

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

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.

Upvotes: 4

Related Questions