Search code examples
iphoneuitableviewnsexception

iphone : proplem with table view


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


Solution

  • 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?