Iphone Objective-C搜索字符串的NSArray?

Iphone Objective-C搜索字符串的NSArray?,iphone,arrays,find,Iphone,Arrays,Find,我有一个字符串数组 我怎样才能知道一个字符串在数组中是什么索引呢?虽然很老了,但这个问题在谷歌搜索中非常重要。这是一个使用block的版本 - (void)testSearch { NSArray *hashAlgorithms = @[@"SHA1", @"SHA2", @"SHA256", @"SHA384", @"SHA512"]; NSString *searchFor = @"SHA384"; __block NSInteger index = NSNotFou

我有一个字符串数组


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

虽然很老了,但这个问题在谷歌搜索中非常重要。这是一个使用block的版本

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

哇!那是。。。简单。哈哈,我会尝试一下,然后再给你回复。只有当你对数组中放置的同一个字符串对象有引用时,这才有效。不仅仅是具有相同值的字符串。当使用
indexOfObject:@“Some string”
时,它将起作用
indexOfObject:
对数组中的对象使用
isEqual:
,并且
NSString的
实现
isEqual:
测试字符串的值,而不是指针;)当我尝试使用从IndexOfoObject返回的值时,我得到一个关于索引超出范围的错误:它说返回的索引是类似201341023之类的粗俗的东西。。。我的数组有3个长度。这个数字实际上是
NSNotFound
,在数组中找不到对象时返回。