0

I have the following situation:

NSArray(
    NSArray(
        string1,
        string2,
        string3,
        string4
    )
    ,
    NSArray(
        string1,
        string2,
        string3,
        string4
   )
)

what I need is a predicate which returns the array that contains a specific string from the objectAtIndex:0 which is related to string1. I should get back the entire array because I need to process the other strings inside that array. Is it possible?

what i am tried to do for now is the following:

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"ANY SELF contains[cd]  %@",searchText];

searchData = [myArray filteredArrayUsingPredicate:resultPredicate];
NSLog(@"array %@",searchData);` 

my problem is my code will return the array when any of string1,string2,string3,string4 contains the searchText and not only string1 and i can't find anyway to achieve my goal!

Larme
  • 24,190
  • 6
  • 51
  • 81
Iphone User
  • 1,930
  • 2
  • 17
  • 26

1 Answers1

0

My opinion is predicates are generally a lose now that we have blocks, especially if you are bumping into problems like this. You can get where you want using:

NSIndexSet *indexes = 
 [myArray indexesOfObjectsPassingTest:^BOOL(NSArray *innerArray, __unused NSUInteger idx, __unused BOOL *stop) {
     // your test on innerArray here, returning BOOL
 }];
NSArray *searchData = [myArray objectsAtIndexes:indexes];

or with a little finagling (this is not a built-in method):

NSArray *searchData = [myArray objectsPassingTest:^BOOL(NSArray *innerArray) {
     // your test on innerArray here, returning BOOL
 }];
Community
  • 1
  • 1
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • Thank you, i found your code very useful for other searches in my application, so i will mark your answer as a solution:) thanks for your help – Iphone User Feb 14 '14 at 09:29