代码之家  ›  专栏  ›  技术社区  ›  Christian Stewart

Objective-C搜索字符串?

  •  4
  • Christian Stewart  · 技术社区  · 14 年前

    我怎样才能知道字符串在数组中是什么索引?

    2 回复  |  直到 12 年前
        1
  •  10
  •   Dave DeLong    14 年前
        2
  •  1
  •   karim    9 年前

    - (void)testSearch
    {
        NSArray *hashAlgorithms = @[@"SHA1", @"SHA2", @"SHA256", @"SHA384", @"SHA512"];
        NSString *searchFor = @"SHA384";
        __block NSInteger index = NSNotFound;
        [hashAlgorithms enumerateObjectsUsingBlock:^(id alg, NSUInteger idx, BOOL *stop) {
            if ([alg compare:searchFor options:NSCaseInsensitiveSearch] == NSOrderedSame) {
                NSLog(@"Found: %@", searchFor);
                *stop = YES;
                index = idx;
            } else {
                NSLog(@"NOT Equal: %@", alg);
            }
        }];
    
        if (index == NSNotFound) {
            NSLog(@"Not found. %li", (long)index);
        } else {
            NSLog(@"Found at: %li", (long)index);
        }
    }