ios 如何检查 NSString 是否包含 NSArray 中的 NSStrings 之一?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4702359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:32:39  来源:igfitidea点击:

How to check if an NSString contains one of the NSStrings in an NSArray?

iosobjective-cnsstringnsarray

提问by Josh

I'm making an iOS app and I need to figure out if an NSStringcontains any of the NSStringsin an NSArray.

我在做iOS应用程序,我需要弄清楚如果NSString包含任何NSStringsNSArray

回答by Nick Moore

BOOL found=NO;
for (NSString *s in arrayOfStrings)
{
  if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
    found = YES;
    break;
  }
}

回答by Adam

It may be a silly optimization for your use case, but depending on how large the array is that you are iterating, it may be helpful/more performant to use NSArray'sindexOfObjectWithOptions:passingTest:method.

对于您的用例来说,这可能是一个愚蠢的优化,但是根据您要迭代的数组有多大,使用NSArray'sindexOfObjectWithOptions:passingTest:方法可能会有所帮助/性能更高。

With this method you pass some options and a block that contains your test. Passing the NSEnumerationConcurrentoption will allow the evaluation of your block to occur on multiple threads concurrently and potentially speed things up. I reused invariant's test, but in a slightly different way. The block functionally returns a BOOL similar to the "found" variable in invariant's implementation. The "*stop = YES;" line indicates that iterating should stop.

使用这种方法,您可以传递一些选项和一个包含您的测试的块。传递该NSEnumerationConcurrent选项将允许您的块的评估同时发生在多个线程上,并可能加快速度。我重用了不变量的测试,但方式略有不同。该块在功能上返回一个类似于 invariant 实现中的“found”变量的 BOOL。“*stop = YES;” 线表示迭代应该停止。

See the NSArray reference documentation for more info. Reference

有关更多信息,请参阅 NSArray 参考文档。参考

NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = ...";
NSUInteger index = [arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent 
                                                passingTest:^(id obj, NSUInteger idx, BOOL *stop) 
                    {
                        NSString *s = (NSString *)obj;
                        if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
                            *stop = YES;
                            return YES;
                        }
                        return NO;
                    }];
if (arrayOfStrings == nil || index == NSNotFound) 
{
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
    return;
}
NSLog(@"The string contains '%@' from the arrayOfStrings", [arrayOfStrings objectAtIndex:index]);

回答by C?ur

Very small security improvement on Adam's answer: there is a big issue with "objectAtIndex:" because it is totally not thread-safe and will make your app crash much too often. So I do:

Adam 回答的安全性改进非常小:“objectAtIndex:”存在一个大问题,因为它完全不是线程安全的,并且会使您的应用程序经常崩溃。所以我这样做:

NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = ...";
__block NSString *result = nil;
[arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent
                             passingTest:^(NSString *obj, NSUInteger idx, BOOL *stop) 
    {
        if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound)
        {
            result = obj;
            *stop = YES;
            //return YES;
        }
        return NO;
    }];
if (!result) 
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
else
    NSLog(@"The string contains '%@' from the arrayOfStrings", result);

回答by Bruno

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", theArray];
BOOL result = [predicate evaluateWithObject:theString];

回答by Alexander

As with the release of iOS8, Apple added a new method to NSStringcalled localizedCaseInsensitiveContainsString. This will exactly do what you want way easier:

随着 iOS8 的发布,Apple 添加了一个新的方法来NSString调用localizedCaseInsensitiveContainsString. 这将完全满足您的要求:

BOOL found = NO;
NSString *string = @"ToSearchFor";
for (NSString *s in arrayOfStrings){
    if ([string localizedCaseInsensitiveContainsString:s]) {
        found = YES;
        break;
    }
}