aqavi_paracha
aqavi_paracha

Reputation: 1131

search arrays with NSPredicate to return results and matches

Hi I'm using NSPredicate to search objects of NSArrays against a 5 elements array, and get the result back. But is there any way to get the matched results returned by NSPredicate as well as the string (among that 5 elements array) against which match succeeded?

Upvotes: 0

Views: 1647

Answers (1)

Balu
Balu

Reputation: 8460

try like this ,

this predicate gives the array which not contains that substring.

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not SELF   contains '%@'",searchString];
        NSArray *array = [NSArray arrayWithObjects:@" AhfjA ", @"test1", @"best", @"AntA", nil];
        NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate ];
        NSLog(@"%@",filteredArray );

O/P:-

(
    AhfjA,
    best,
    AntA
)

it gives the array which contains the substring

 NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"SELF   contains '%@'",searchString];
            NSArray *array = [NSArray arrayWithObjects:@" AhfjA ", @"test1", @"best", @"AntA", nil];
            NSArray *filteredArray  = [array filteredArrayUsingPredicate:predicate ];
            NSLog(@"%@",filteredArray );

O/P:-

(

    test1,

)

Upvotes: 1

Related Questions