Search code examples
iphoneobjective-ciosnsindexset

Search index of NSMutableArray


I need to search the index of a string from NSMutableArray. I have implemented the code & which works perfect, but I need to increase the searching speed than this.

I have used the following code:

NSIndexSet *indexes = [mArrayTableData indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
    NSString *s = (NSString*)obj;
    NSRange range = [s rangeOfString: txtField.text options:NSCaseInsensitiveSearch];
    if(range.location == 0)//
        return range.location != NSNotFound;
    return NO;
}];

NSLog(@"indexes.firstIndex =%d",indexes.firstIndex);

Solution

  • There is a method indexOfObject

    NSString *yourString=@"Your string";
    NSMutableArray *arrayOfStrings = [NSMutableArray arrayWithObjects: @"Another strings", @"Your string", @"My String", nil];
    
    NSInteger index=[arrayOfStrings indexOfObject:yourString];
    if(NSNotFound == index) {
        NSLog(@"Not Found");
    }