0
votes

I’ve got this error when populating a tableview from an nsmutablearray.

The arrays are added to the dictionary and the dictionary added to the mutable array, but the app crashes each time the view is loaded with error:

[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x8a87e60 2014-05-24 12:23:02.589 jwStudy[77652:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x8a87e60'

The log output confirms that the arrays and keys are added correctly. But why does the cell in the cellForRowAtIndexPath: method throw an error? Here’s the code:

- (void)viewDidLoad
{
    [super viewDidLoad];


    languageArray = [[NSMutableArray alloc] init];
    languages = [[NSArray alloc] initWithObjects:@"Abbey", @"Abkhasisk", @"Abua", nil];

    downloadURL = [[NSArray alloc] initWithObjects:@"http://download.jw.org/files/content_assets/bb/502013341_ABB_cnt_1_r480P.mp4",
                   @"http://download.jw.org/files/content_assets/3c/502013341_ABK_cnt_1_r480P.mp4",
                   @"http://download.jw.org/files/content_assets/7e/502013341_AU_cnt_1_r720P.mp4", nil];

    mutedict = [NSDictionary dictionaryWithObjectsAndKeys:languages, @"FirstKey", downloadURL, @"SecondKey", nil];
    [languageArray addObject:mutedict];

   NSLog(@"%@",languageArray);

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return languageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // ERROR HERE!!!
    cell.textLabel.text =  [[languageArray objectAtIndex:indexPath.row] objectForKey:@"FirstKey”];

    if ([checkCellLanguage isEqual:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}
2
Is your checkCellLanguage of the type NSInteger ? also, objectForKey method can only be called on the instance of NSDictionary. You should store your array object into dictionary and not dictionary into array. After which you can use objectForKey on the NSDictionary object. - Farhan Misarwala
You can ignore the checkCellLanguage variable. That is just for adding a checkmark for the selected row. I'm a bit confused about storing the array object the right way. Can you elaborate on this? - Philip Borges
sure. Your arrayLanguage variable is going out of bounds since its allocating (entire *mutedict) just one object at index 0. you can allocate the dictionary by using a for loop where the limit for the loop can be the size of the array which you want to assign. you can see this link. It can help. - Farhan Misarwala
Thanks Farhan. I'll take a look at it. - Philip Borges

2 Answers

1
votes

@Farhan and Joy.

Thank you both for prompt reply. I have realized now that I wasn't clear enough in my question. What I wanted was for each selected row in the tableview to register the row's corresponding URL. So I ended up abandoning the arrays and dictionaries ... too confusing. And going with 2 NSArrays instead. This link got me going on the right track and everything works now:

http://sweettutos.com/2012/08/25/loading-urls-from-uitableview/

0
votes

In your code, two arrays stored in a dictionary , and the dictionary stored in a array.

Like this :

languageArray ----- mutedict ------ languages (FirstKey)

                       |
                       |
                        ----------- downloadURL(SecondKey)

cell.textLabel.text is a NSString, but [[languageArray objectAtIndex:indexPath.row] objectForKey:@"FirstKey”]; is NSArray

So your code throw an error.

If your want put a language as cell.textLabel.text

You could modify your code as cell.textLabel.text = [[mutedict objectForKey:@"FirstKey"] objectAtIndex:indexPath.row]

Hope it can help you :)