0

I have a problem with table view in iphone .. i can't figure out why it crashes everytime will here is the code

   - (void)viewDidLoad
{
     [self checkAndCreatePList];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];

    self.animals = [plistDict objectForKey:@"Animals"];



         [super viewDidLoad];

}



    -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell== nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier]autorelease];
    }
    NSUInteger row = [indexPath row];

    cell.textLabel.text = [animals objectAtIndex:row];

    return cell;
}

it's crashes at the line cell.textLabel.text = [animals objectAtIndex:row]; and tells me that Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64
  • what is the content of file at pListPath? – saadnib Jun 10 '11 at 13:36
  • Try NSLogging `[animals objectAtIndex:row]`, if it's wrong, try NSLogging `self.animals`, if that's wrong, use: `NSLog(@"%@", [plistDict]);`. Use this to backtrace what's wrong in the storage. The code seems ok to me. – Joetjah Jun 10 '11 at 13:38
  • IS animals is an array or dictionary – Rams Jun 10 '11 at 13:38
  • Oh, and is `pListPath` the link to a NSDictionary? – Joetjah Jun 10 '11 at 13:40
  • ok thanks every one for your fast help.. it seems that i need some sleep here.. but before that.. can anyone give me a link to how to make a table with let's say some categories and under every category we have list of string from the plist for example.. i hope it's easy for you thanks again you saved my day – Mohamed Emad Hegab Jun 10 '11 at 14:12

4 Answers4

2

The Animals key in your plist refers to a dictionary, not an array. Dictionaries don't have a guaranteed order, so asking for an object at a particular index doesn't make sense.

In addition to this, you have a memory leak - plistDict is allocated but never released. Have you run the static analyser over your code?

Jim
  • 72,985
  • 14
  • 101
  • 108
0

[plistDict objectForKey:@"Animals"];

is returning a Dictionary not an Array like you are expecting. You need to check out your plist file to see if the data is correct.

MarkPowell
  • 16,482
  • 7
  • 61
  • 77
0

the error seems that you are calling objectAtIndex on a NSDictionary object at line cell.textLabel.text = [animals objectAtIndex:row]; check what does animal contains at run time. For this use NSLog before this line. NSLog(@"%@",animals);

saadnib
  • 11,145
  • 2
  • 33
  • 54
0

Looks like animals is some dictionary and you are calling objectAtIndex: method on it. objectAtIndex: is NSArray method.