【问题标题】:Find a substring in NSMutableArray of string fast在字符串的 NSMutableArray 中快速查找子字符串
【发布时间】:2014-10-30 07:19:24
【问题描述】:

我有一个类似的数组

NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"How many degrees ", @"Which club degree", @"body building", nil];

只想过滤那些包含度数的字符串。我用的是老IOS,没有[string stringcontains]方法

所需的数组必须有 -> 1. 多少度 2. 哪个俱乐部度

我使用的第一种方法

NSString *strToMatch=@"degree";

    for (NSString *description in arr)
    {
     NSComparisonResult result = [description compare:strToMatch options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [strToMatch length])];

           if (result == NSOrderedSame)
              {
                 // statement to run on comparison succeeded..
              }
    }

第二种方法

for (NSString *description in arr)
   {

      if ([description rangeOfString:strToMatch options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            NSLog(@"I am matched....");
        }
  }

它工作正常。我已经向您展示了比较单个字符串的代码,但我必须将此数组与另一个字符串数组进行比较。我想加快我的代码。有没有其他更好的方法可以找到它。

我们如何创建一个 NSPredicate 来比较两个字符串数组。在第二个数组中查找第一个字符串数组作为子字符串。

哪种方法快。

提前致谢。

【问题讨论】:

    标签: ios objective-c nsmutablearray string-comparison


    【解决方案1】:

    您可以使用 NSPredicate-[NSArray filteredArrayUsingPredicate:],因为它在 iOS 3.0 中可用

    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"How many degrees ", @"Which club degree", @"body building", nil];
    NSString *searchText = @"degree";
    NSArray *filtered = [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self contains %@", searchText]];  
    

    也可用于不区分大小写和不区分变音符号的搜索使用

    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"How many degrees ", @"Which club degree", @"body building", nil];
    NSString *searchText = @"deGree";
    NSArray *filtered = [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self contains[cd] %@", searchText]];  
    

    如果您想在另一个数组中搜索关键字数组,您可以使用NSCompoundPredicate,它在 iOS 3.0 中也可以使用。

    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"How many degrees ", @"Which club degree", @"body building", @"hello world" , nil];
    NSArray *keywordsToSearch = @[@"deGree", @"world"];
    NSMutableArray *predicates = [NSMutableArray new];
    for (NSString *keyword in keywordsToSearch) {
        [predicates addObject:[NSPredicate predicateWithFormat:@"self contains[cd] %@", keyword]];
    }
    NSPredicate *wholePredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
    
    NSArray *filtered = [arr filteredArrayUsingPredicate:wholePredicate];
    

    您还可以检查性能并选择最佳的。

    NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
    // .... here searching code
    NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
    NSLog(@"Duration %f", endTime - startTime);
    

    【讨论】:

    • 运行完美,但告诉我哪种方法快,何时在 NSPredicate 或我的方式之间进行选择..
    • 我觉得苹果的实现应该会更快,但反正我已经添加了性能代码,检查我编辑。
    • 谢谢,我是IOS新手,你能给我推荐一些网站或教程,通过它们我可以增强我对IOS的概念。不是代码。我想学习概念。
    • 我认为 WWDC 视频会很有帮助(尤其是“高级”视频)developer.apple.com/videos/wwdc/2014 也可以查看developer.apple.com/library/ios/navigation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2011-07-13
    • 1970-01-01
    • 2014-05-24
    • 2015-08-18
    相关资源
    最近更新 更多