我需要从NSMutableArray搜索字符串的索引。我已经实现了代码&可以完美地工作,但是我需要提高搜索速度。

我使用了以下代码:

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

最佳答案

有一种方法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");
}

关于iphone - NSMutableArray的搜索索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11940857/

10-14 22:25