Search code examples
iosjsonnsdictionaryreserved-words

How to access to a NSdictionary element with reserved keyword?


My code receive a json data from server and present into a UItable. here it is the format of my josn file from the server:

[
  {
    "id": 4342,
    "name": "Strawberries and Cream Cake",
    "difficulty": 3,
    "created_at": "2014-09-29T10:43:00.072Z",
    "updated_at": "2014-11-26T11:53:58.451Z",
  }
]

I need to access to "id" element, I write following code in .

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

NSDictionary *tempDictionary=[recipes objectAtIndex:indexPath.row];
int recipesID= [tempDictionary objectForKey:@"id"];

This code gives me the a wrong number, plus the second line gets a warring for "incompatible pointer to integer conversion initializing.... I know that id is a reserved keyword, but I need to access this element. beside I did not implement the server so, I can not change id element name. Do you have any suggestion how to access it ?


Solution

  • Replace your last line with this:

    NSInteger recipesID= [[tempDictionary objectForKey:@"id"] intValue];
    

    By the way, id itself is indeed a reserved keyword on iOS, however, you are using it in a string, so it's not interpreted as a keyword by the compiler :) So, no reason to worry about this here.