3

I am trying to set text to my labels. Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    newsDataCell *cell = (newsDataCell*)[tableView dequeueReusableCellWithIdentifier:@"newsData"];
    
    if (cell == nil)
    {
        cell = [[newsDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsData"];
    }
    
   NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
     NSLog(@"currentrow:%@",currentrow);
    //UIImageView *image = (UIImageView*)[cell viewWithTag:0];
    
    UILabel *txtData = (UILabel*)[cell viewWithTag:1];
    NSString *testo = [currentrow objectForKey:@"Date"];
    [txtData setText:testo];
    
    
    UILabel *txtTitolo = (UILabel*)[cell viewWithTag:2];
    txtTitolo.text =[currentrow valueForKey:@"Title"];

    UILabel *txtDescrizione = (UILabel*)[cell viewWithTag:3];
    txtDescrizione.text = [currentrow valueForKey:@"Descr"];

    return cell;
}

I checked and currentrow actually contains the object. This is the error I get from Xcode:

2013-09-11 16:04:36.468 ApplicationName[2409:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7172f80' *** First throw call stack: (0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227 0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55) libc++abi.dylib: terminate called throwing an exception

I get the same error using valueForKey:

txtTitolo.text =[currentrow valueForKey:@"Title"];

Here it is:

2013-09-11 16:09:34.478 ApplicationName[2409:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM valueForKey:]: unrecognized selector sent to instance 0x7172f80' *** First throw call stack: (0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227 0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55) libc++abi.dylib: terminate called throwing an exception

What is wrong?

peterh
  • 11,875
  • 18
  • 85
  • 108
nicky_1525
  • 929
  • 1
  • 10
  • 24

4 Answers4

0

A. This error is not produced by Xcode but by the runtime environment.

B. It means that currentrow is not a dictionary but an array.

C. How did you check it? Where do you create the items and put it in the list?

bneely
  • 9,083
  • 4
  • 38
  • 46
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
0

Looks like currentrow is not a NSDictionary (which you are assuming). It is an NSArray.

NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
// Not a dictionary, it is an array.

That is why you are getting this crash. If this is a parsed JSON response, check the structure of JSON.

Hope that helps!

Amar
  • 13,202
  • 7
  • 53
  • 71
  • Even if it is an Array "valueForKey" should work and retrieve the string.Am I wrong?? – nicky_1525 Sep 11 '13 at 14:41
  • @nicky_1525 The `valueForKey:` that you are talking about is not supposed to be used as you are trying. It is meant for Key-Value Observation(KVO) coding. – Amar Sep 11 '13 at 14:45
  • Ok, so other suggestions?How can I retrieve the strings? – nicky_1525 Sep 11 '13 at 15:02
  • @nicky_1525 What is the structure? Is `currentrow` supposed to be array of dictionary or something else? – Amar Sep 11 '13 at 15:04
  • Currentrow is supposed to contain all keys and values for the object at the row index of the tableview. If so I could get values and populate all my labels in my tableviewcell. – nicky_1525 Sep 11 '13 at 15:15
  • @nicky_1525 Could you add the JSON/`self.Items` log in your question? I will get a better idea. – Amar Sep 11 '13 at 15:17
  • @nicky_1525 /value/ForKey: will work, but returns an array applied on arrays. This is documented. – Amin Negm-Awad Sep 11 '13 at 19:59
0

I did it! I just used "stringWithFormat" and it worked like this:

NSString *testo = [NSString stringWithFormat:@"%@",[currentrow objectForKey:@"Date"]];

Thank you anyway for your answers!

nicky_1525
  • 929
  • 1
  • 10
  • 24
0

For me this is worked.

customerBillerFieldsArray = [self.detailItem valueForKey:@"customerBillerFileds"];

Earlier I used customerBillerFieldsArray = [self.detailItem objectForKey:@"customerBillerFileds"];

Narasimha Nallamsetty
  • 1,215
  • 14
  • 16