Ketan Ketan - 5 months ago 22
Objective-C Question

filter array of dictionaries with two strings

For search dropdown table I need to show the "Description" and "SearchTerms" in cell textlabel and detailed Textlabel respectively. I need to implement the filter which is based on two strings i.e search text should be compare with the both "Description" and "SearchTerms". I tried NSPredicate but it can filter for one string only

NSString *substring = [NSString stringWithString:searchServiceTextField.text];
NSLog(@"substring %@",substring);
NSMutableArray *arr2Filt= [searchArray valueForKey:@"Description"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",substring];
NSArray *filteredarr = [NSMutableArray arrayWithArray:[arr2Filt filteredArrayUsingPredicate:predicate]];
NSLog(@"filtered array %@",filteredarr);


i tried to modify for both the strings but i didn't get better soultion.

[{
Description = "Rental Jewellery, Jewellery for events";
ProfessionId = "<null>";
SKUFormat = "<null>";
SearchTerms = "Rental Jewellery";
SpecialityId = 62;
tokens = "<null>";
value = "<null>";
},
{
Description = "Kids Party with Food and Games Venues";
ProfessionId = "<null>";
SKUFormat = "<null>";
SearchTerms = "Party Venue";
SpecialityId = 63;
tokens = "<null>";
value = "<null>";
},
{
Description = "Music, Dance, Sports classes";
ProfessionId = "<null>";
SKUFormat = "<null>";
SearchTerms = Classes;
SpecialityId = 64;
tokens = "<null>";
value = "<null>";
},
{
Description = "Zumba, Yoga, Aerobics classes";
ProfessionId = "<null>";
SKUFormat = "<null>";
SearchTerms = "Fitness/Health";
SpecialityId = 65;
tokens = "<null>";
value = "<null>";
}
]

Answer

you can do like

NSString *substring = [NSString stringWithString:searchServiceTextField.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Description contains[c] %@ AND SearchTerms contains[c] %@",substring, substring];
NSArray * filteredarr =[[searchArray filteredArrayUsingPredicate:predicate] copy];

NSLog(@"filtered array %@",filteredarr);
Comments