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 :)

Dani AI

Generated

— short diagnosis and two clean fixes.

The plist you showed has an array of dictionaries (each item contains the key "Cell"). The code in the thread treats each row as an NSString (you run a SELF beginswith… predicate and set textLabel.text directly from the array). That mismatch (NSDictionary vs NSString) is the usual cause of the crash or empty results — commonly seen as an unrecognized selector like -[__NSDictionaryM length] in the log.

Fix A — build a plain string array (easy drop-in)

NSString *p = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSData *data = [NSData dataWithContentsOfFile:p];
id root = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
if ([root isKindOfClass:[NSArray class]]) {
    NSArray *rows = root; // array of dictionaries
    NSMutableArray *titles = [NSMutableArray arrayWithCapacity:rows.count];
    for (NSDictionary *item in rows) {
        NSString *txt = item[@"Cell"];
        if ([txt isKindOfClass:[NSString class]]) [titles addObject:txt];
    }
    self.array = [titles copy]; // now an array of NSStrings — predicates and table text will work
}

Fix B — keep dictionaries and predicate on the key (no mapping)

NSPredicate *pred = [NSPredicate predicateWithFormat:@"Cell CONTAINS[cd] %@", searchText];
self.searchResults = [rows filteredArrayUsingPredicate:pred]; // rows is the array of dicts

Then adapt cellForRow to read the key:

NSDictionary *row = tableView == self.searchDisplayController.searchResultsTableView ? self.searchResults[indexPath.row] : rows[indexPath.row];
cell.textLabel.text = row[@"Cell"];

Extra troubleshooting notes: confirm the plist is actually bundled (target membership), print the loaded object (NSLog(@"%@", root)) to verify structure, use NSLog(@"count: %lu", (unsigned long)rows.count) for correct formatting, and make sure the storyboard/table cell reuse identifier matches "cellID". Also note UISearchDisplayController is deprecated in iOS 8+ — consider migrating to UISearchController for newer projects.

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.