Search code examples
iosobjective-cuitableviewnsarraynsdictionary

Convert NSDictionary to NSArray


I'm trying to convert my NSDictionary (storageData) to an NSArray, as I need to pass the data (title) contained in the dictionary through a segue. I've implemented the below (see last method, the tableview is simply to provide some context), but a crash occurs with the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance

Any idea how to fix?

Viewcontrolller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";

    StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
    if (cell == nil)

        {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

            NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:@"%d",(long)indexPath.row]];

        [[cell itemName] setText:(NSString *)[tmp objectForKey:@"title"]];

              NSString *title = [tmp objectForKey:@"title"];
              [[cell itemName] setText:title];

 NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
 [[cell itemDescrip] setText:[node objectForKey:@"body"]];

NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:@"photo"];

[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];

              }

              return cell;
              }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray *values = [self.storageData allKeys];

    ItemDetailViewController *detailViewController = [[ItemDetailViewController alloc]
                                                                   initWithNibName:@"ItemDetailViewController" bundle:nil];

    detailViewController.title = [[values objectAtIndex:indexPath.row] objectForKey:@"title"];
    detailViewController.itemDetail = [self.descripData objectAtIndex:indexPath.row];
    detailViewController.secondLink = self.descripData[indexPath.row][@"photo"];
    [self.navigationController pushViewController:detailViewController animated:YES];
       }

Solution

  • I believe if you would extract allValues instead of allKeys then you could only:

    [values objectAtIndex:indexPath.row] 
    

    Since you are using a index as indexPath.row. Let me know if this helps.