1

I have an array like

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

A want to filter only those string contains degree. I am using old IOS, i don't have [string stringcontains] method

The required Array must have -> 1. How many degrees 2. Which club degree

First Method I use

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..
              }
    }

Second Method

for (NSString *description in arr)
   {

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

It is working fine. I have shown you the code to compare a single string but i have to compare this array with another array of string. I want to speed up my code. Is there any other better approach to find it.

And how can we create a NSPredicate to compare two array of string. Find first arrray of string as a substring in second array.

And which approach is fast.

Thanks in Advance.

1 Answer 1

3

You can use NSPredicate and -[NSArray filteredArrayUsingPredicate:] as it's available from 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]];  

Also for case-insensitive and diacritic-insensitive search use

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]];  

If you want to search array of keywords in another array, you can use NSCompoundPredicate which is also available from 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];

Also you can check performance and choose the best one.

NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
// .... here searching code
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSLog(@"Duration %f", endTime - startTime);
Sign up to request clarification or add additional context in comments.

4 Comments

running perfectly but tell me which method is fast and when to chose between NSPredicate or my way..
I think Apple's implementation should be faster, but anyway I have added performance code, check me edit.
Thanks,I am new to IOS, can you suggest me some sites or tutorial through which i can enhance my concepts about IOS. Not code.I want to learn concepts .
I think WWDC videos can be very helpful (especially 'Advanced' videos) developer.apple.com/videos/wwdc/2014 also you can check developer.apple.com/library/ios/navigation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.