Search NSString in NSMutableArray
ios, nsmutablearray, nspredicate, nsstring, objective-c
Solution
Finally I drop the idea of apply search and match the string name with the searched text, and if the Name contains the searched text then that array index is added to the filtered array.
Here is the code for the same :
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",self.strSearchingText];
NSLog(@"string :%@",Predicate);
NSLog(@"arrayPeoples count is : %d",[self.arrayPeoples count]);
NSMutableArray *arrSearch = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.arrayPeoples count];i++ )
{
ParsingItem *item = [self.arrayPeoples objectAtIndex:i];
NSString * strUserName = [item.mdictXMLTagsData valueForKey:@"UserName"];
if ([strUserName rangeOfString: strSearchingText options: NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog (@"Yay! '%@' found in '%@'.", self.strSearchingText, strUserName);
[arrSearch addObject:item];
}
}
self.filteredArray = arrSearch;
Problem
This may be possible duplicate of many existing questions, one of them is : This or other links that I have searched so far. I'm working on an application in which I'm displaying People Information like thier Location, Name, Image, Gender, Phone Number etc. I am getting this data through xmls. From Parsing I feed my NSMutableArray and display the details on my table view. I want to Apply Search on People NAME in this array. How do I get People Names from these ? Here is the code I am using : For displaying details in Table View : ``` if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellDetail" owner:self options:nil]; id firstObject =[topLevelObjects objectAtIndex:0]; if ( [ firstObject isKindOfClass:[UITableViewCell class]] ) cell = (CustomCellDetail *)firstObject; ParsingItem *item=[self.arrayPeoples objectAtIndex:indexPath.section]; cell.lblUserName.text=[item.mdictXMLTagsData valueForKey:@"UserName"]; cell.lblStatus.text=[item.mdictXMLTagsData valueForKey:@"StatusMessage"]; cell.lblAge.text=[item.mdictXMLTagsData valueForKey:@"Gender"]; ``` and for fetching results I found that only string can be searched from nsmutable array so I applied search as follows : ``` -(void)FetchSearcheRecordsFromArray { self.filteredArray = self.arrayPeoples; NSLog(@"string :%@",strSearchingText); NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",self.strSearchingText]; NSLog(@"string :%@",Predicate); [self.filteredArray filterUsingPredicate:Predicate]; NSLog(@"Array count is : %d",[self.filteredArray count]); if ([self.filteredArray count] <= 0 ) { self.lblNoRecordsFound.hidden = NO; } else { self.lblNoRecordsFound.hidden = YES; } [self.tblViewPeople reloadData]; } ``` My Application Crashes on line : ``` [self.filteredArray filterUsingPredicate:Predicate]; ``` As I know that the string is not present in my array and there is a parsing item. I want to apply search on only Names of the people but I dont know how to get only name from array. How can I achieve this ??? Any work arounds?? I need this to be done but dont know how !! Please help !