Search code examples
iosobjective-csearchplistuisearchcontroller

How to implement searching a string in plist?


I tried all things but it's not working. I have tableview with loads plist. I'm trying to implement the search function.

there is my code,

#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic)NSMutableArray *itemList;
@property (nonatomic)NSMutableArray *filteredNames;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) UISearchController *searchController;

When I search something, I can see entities in nslog function but there are no results in tableview

-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(nonnull UITableView *)tableView
{
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

    [_filteredNames removeAllObjects];
    if (searchString.length > 0) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@",self.searchBar.text];
        NSLog(@"%@",predicate);

        NSString *filePathBundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
        NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:filePathBundle];
        NSArray *films = [root objectForKey:@"FilmsPlaying"];
        for (NSDictionary *dic in films) {

            Films *f = [[Films alloc] initWithDictionary:dic];
            [_filteredNames addObject:f];

        }
    }

        return YES;

}

there is my .m file

- (void)viewDidLoad {
    [super viewDidLoad];

    _searchController = [[UISearchController alloc]init];

    self.filteredNames = [[NSMutableArray alloc]init];


    [self.tableView  dataSource];
    [self.tableView delegate];
    [self.tableView reloadData];
}

and there is my plist;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>FilmsPlaying</key>
    <array>
        <dict>
            <key>ZFILMTIME</key>
            <integer>90</integer>
            <key>ZFILMDATESTART</key>
            <date>2015-04-01T09:15:42Z</date>
            <key>ZFILMCONTENT</key>
            <string>bla bla bla </string>
            <key>ZFILMDIRECTOR</key>
            <string>Don Hall,Chris Williams</string>
            <key>ZFILMID</key>
            <string></string>
            <key>ZFILMIMAGE</key>
            <string>bla.jpg</string>
            <key>ZFILMNAME</key>
            <string> Big Hero 6</string>
            <key>ZFILMTYPE</key>
            <string>bla bla bla </string>
            <key>ZFILMVOTE</key>
            <integer>10</integer>
        </dict>
        <dict>
            <key>ZFILMTIME</key>
            <integer>90</integer>
            <key>ZFILMDATESTART</key>
            <date>2015-04-01T09:15:42Z</date>
            <key>ZFILMCONTENT</key>
            <string>bla bla bla .</string>
            <key>ZFILMDIRECTOR</key>
            <string>Frankie Chung</string>
            <key>ZFILMID</key>
            <string></string>
            <key>ZFILMIMAGE</key>
            <string>bla.jpg</string>
            <key>ZFILMNAME</key>
            <string>bla bla bla </string>
            <key>ZFILMTYPE</key>
            <string>bla bla bla </string>
            <key>ZFILMVOTE</key>
            <real>9.5</real>
        </dict>
</array>
</dict>

I'm waiting for your advice, thanks


Solution

  • you search Array (films) of NSDictionary so use you use SELF.keyname from NSDionary in your predicate

    NSString *filePathBundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:filePathBundle];
    NSArray *films = [root objectForKey:@"FilmsPlaying"];
    
    NSLog(@"%@",films);
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.ZFILMNAME CONTAINS[cd] %@", self.searchBar.tex];
    
    NSArray *searchResult = [films filteredArrayUsingPredicate:predicate];
       NSLog(@"%@",searchResult);