Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the index of an NSString NSArray, matching a specific string?

I have an array of NSString.

exampleArray = @[@"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20"];

Let's say you have..

NSString *exampleString = @"7";

I want to find exampleString, in exampleArray, and have it return the index of 2, the index where @"7" is located.

What is the best way to do this besides looping through and comparing isEqualToString?

like image 597
chris P Avatar asked Oct 10 '14 05:10

chris P


1 Answers

if([exampleArray containsObject:exampleString]) {
    int index = [exampleArray indexOfObject: exampleString];
} else {
    NSLog(@"not found");
}
like image 157
Balu Avatar answered Mar 03 '23 10:03

Balu