Finding NSString in NSMutableArray

This may be a duplicate of many existing issues, one of which is: This

or other links I've been looking for so far.

I am working on an application in which I display user information such as Location, Name, Image, Gender, Phone Number, etc.

I get this data through xmls. From parsing, I submit my NSMutableArray and show the details in the table view.

I want to apply a search in the NAME of a user in this array.

How do I get the names of people from them?

Here is the code I'm using:

To display information in a table:

 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 to get the results, I found that only a string can be searched from an nmutable array, so I applied the 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];

, , .

, , .

?

?

, , , .

, !

+3
4

, , , .

:

   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;
+3

enumerateObjectsUsingBlock::

self.lblNoRecordsFound.hidden = YES;
[self.filteredArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj rangeOfString: strSearchingText options: NSCaseInsensitiveSearch].location != NSNotFound) {
        self.lblNoRecordsFound.hidden = NO;
        *stop = YES;
    }
}];

, ( ). , .

0

, , . contains - ParsingItems, ParsingItem .

ParsingItem, , ( ParsingItems, , , UserName ParsingItem mdictXMLTagsData), - :

[NSPredicate predicateWithFormat:@"mdictXMLTagsData.UserName contains[c] %@", self.strSearchingText]
0

, ParsingItem s.

, . , , .

Spend some time - to read (and understand) Apple documentation NSArray, NSPredicate. Read the documentation for the classes you use to parse your XML. Go through your code to understand what it does. Add NSLogif this helps. Maybe write a test application that does nothing but parse your XML.

Then, when you have done all this, and if you still do not understand, come back and ask for help.

0
source

All Articles