开发者

Objective-C Search NSArray for String?

开发者 https://www.devze.com 2023-01-13 04:54 出处:网络
I have an array of strings. How might I be 开发者_开发问答able to figure out what index a string is in a array?-[NSArray indexOfObject:]Though very old but this question comes quite high in google se

I have an array of strings.

How might I be 开发者_开发问答able to figure out what index a string is in a array?


-[NSArray indexOfObject:]


Though very old but this question comes quite high in google search. Here is a version in using 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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消