2010-08-27 61 views

回答

10
+0

哇?那是......簡單。哈哈,我會試一試並回復你。 – 2010-08-27 21:51:36

+0

如果你有一個對數組中放入的同一個字符串對象的引用,這隻會起作用。不只是一個具有相同值的字符串。 – 2010-08-27 21:56:05

+7

使用'indexOfObject:@「一些字符串」''時它會工作! 'indexOfObject:'在數組中的對象上使用'isEqual:',''isEqual:'測試字符串的值,而不是指針的NSString'') – 2010-08-27 22:11:10

1

雖然很老,但這個問題在談到谷歌的搜索相當高。這裏是一個使用版塊,

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