Question

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

Was it helpful?

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?

OTHER TIPS

[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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top