0
votes
SOffer[26229:c07] current Product: (
    {
        id = 20;
        image = "img2.png";
        name = "offer 2";
    }
)

I have product which result the above when I print it through NSLog,, I need to print the these individually. Following code generate this

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"cell"];
        }
        NSString *currentProductName;
        currentProductName = [productKeys objectAtIndex:indexPath.row];

        currentProduct = [products objectForKey:[productKeys objectAtIndex:indexPath.row]];
        NSLog(@"current Product: %@",currentProduct);
        //currentProductName = [currentProduct objectForKey:@"image"];
        //currentProduct = [currentProduct ];

        [[cell textLabel] setText:currentProductName];

    return cell;
    } 

currentProduct is declared as NSArray but when i try to print with objectForKey it say No visible interface for NSArray declares the selector ObjectForKey

1
objectForKey i believe works with NSDictionary only. NSArray does not have key value pairs. Unless currentProduct holds the JSON file, then you gonna have to first get out the Array Object of the first Product and assign it to a NSDictionary, where you can then use the objectForKey to get your image valueSteven
thanks, how i will extract the id, name and image separately from array?MIrfan
Look at the answer I postedSteven
NSDictionary *cp = [products objectForKey:[productKeys objectAtIndex:indexPath.row]]; I did this way, it compile good but terminate execution with this message in console -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x759d140MIrfan
seems like your objectForKey is receiving a value that is not expected. Separate your two calls into different lines and do a NSLog to see if those are the values you expectSteven

1 Answers

1
votes

objectForKey, I believe works with NSDictionary only.

NSArray does not have key value pairs. Unless currentProduct holds the JSON file, then you gonna have to first get out the Array Object of the first Product and assign it to a NSDictionary, where you can then use the objectForKey to get your image value.

Something like this:

// Get the first product object from the NSArray. Assuming you dropped your entire Product values into a NSArray
NSMutableDictionary *prodDict = [currentProduct objectAtIndex:indexPath.section];
NSString *prodID = [prodDict objectForKey:@"id"];
NSString *prodName = [prodDict objectForKey:@"name"];
...