2010-08-27 59 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); 
    } 
}