Can anyone help me? I have created a dictionary with the contents of a plist(data.plist) file but now I cannot figure out how to search and display the contents. I have searched online for days and cannot get a resolution.

Most tutorials show a pre-defined array being used but if I change it to my plist it crashes.

Any help would be greatly appreciated. I posted my code below here.

//
//  ViewController.h
//  Search
//
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>
@end

Here is my .m file as well.

//
//  ViewController.m
//  Search
//
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) NSArray *arrayCount;
@property (strong, nonatomic) NSArray *searchResults;
@property (strong, nonatomic) NSMutableArray *filteredTableData;
@end
@implementation ViewController
int arrayCounter = 0;
NSString *test = @"";
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
    self.arrayCount = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
    NSLog(@"Array size: %d", [self.arrayCount count]);
    //self.array = [[NSArray alloc] initWithObjects: @"Test", @"Test 2", @"Test 3", nil];
    self.array = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
    self.searchResults = [[NSArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma Table View Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];
    } else {
        return [self.array count];
    }
//    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    }
//    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    return cell;
}
#pragma Search Methods
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
//    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
    self.searchResults = [self.array filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    return YES;
}
@end

Data.plist:
Root
Item 0 - Dictionary
Cell - String ( Bob )
Item 1 - Dictionary
Cell - String ( Bob 2 )
Item 2 - Dictionary
Cell - String ( Bob 3 )

And see if I populate the array with objects instead of withcontents of file it functions fine and populates the results.

Thanks for looking into this :)

Anyone? :( I am still trying to research this but am having no luck using data located in a plist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.