Search code examples
iosarraysnspredicatefast-enumeration

Search in Array (from plist with dictionaries) with Fast enumeration or NSPredicate


My app reads data from a plist file and displaying the data in a UITableView

my plist EXAMPLE:

<array>
<dict>
      <key>name</key>
      <string>One</string>
      <key>value</key>
      <string>1</string>
</dict>
<dict>
      <key>name</key>
      <string>Two</string>
      <key>value</key>
      <string>2</string>
</dict>
<dict>
      <key>name</key>
      <string>Three</string>
      <key>value</key>
      <string>3</string>
</dict>
<dict>
      <key>name</key>
      <string>Four</string>
      <key>value</key>
      <string>4</string>
</dict>
<dict>
      <key>name</key>
      <string>Five</string>
      <key>value</key>
      <string>5</string>
</dict>
<dict>
      <key>name</key>
      <string>Six</string>
      <key>value</key>
      <string>6</string>
</dict>
</array

Everything now works fine. Everything is displayed in my TableView with right detailed cells. I'm displaying the value in the detailTextLabel and the name in TextLabel.

if (isSearching==true) {
    cell.textLabel.text = [searchArray objectAtIndex:indexPath.row];
//How to include the key "value" from this non-dictionary Array?
    return cell;
}

cell.textLabel.text = [[mainArray objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text = [[mainArray objectAtIndex:indexPath.row] objectForKey:@"value"];

and for numbersOfRowsInSection of course :

if (isSearching) {
    return [searchArray count];
}
return [IDarray count];

i'm using this to get an array from the plist:

NSMutableArray *mainArray = [[NSMutableArray alloc] initWithContentsOfFile:path-to-plist-file];

but then I want to add a UISearchBar to search through my data (array).

I have watched and read tutorials but I can't get it to work with Fast Enumeration or NSPredicate, More alternatives?

Because i'm reading data from plist with many dictionaries?

so far i tested this without success:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length==0) {
    isSearching = false;
}

else {
    isSearching=true;
    searchArray = [[NSArray alloc]init];

    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", searchText];
    searchArray = [mainArray filteredArrayUsingPredicate:pre];
}
[tableView reloadData]; 
}

This will display "no results" when searching and sometimes it will crash.

I also tested:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length==0) {
    isSearching = false;
}

else {
    isSearching=true;
    searchArray = [[NSArray alloc]init];

    for (NSString *str in mainArray) {
        NSRange searchRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];


        if (searchRange.location !=NSNotFound) {
            [searchArray addObject:str];
        }
}
[tableView reloadData]; 
}

doesn't work...

How can I implement the Search Bar to search through data? and how can I display the value and the name in search display controller?


Solution

  • You have array of dictionaries in mainArray. It doesn't work:

    for (NSString *str in mainArray)
    

    try so:

    id mainArray = [[NSMutableArray alloc] initWithContentsOfFile:path-to-plist-file];
    NSMutableArray *names = [mainArray valueForKey:@"name"];
    NSMutableArray *values = [mainArray valueForKey:@"value"];
    

    and seeking in necessary array