如何使用保留关键字访问 NSdictionary 元素?

How to access to a NSdictionary element with reserved keyword?

我的代码从服务器接收 json 数据并呈现到 UItable 中。这是我来自服务器的 josn 文件的格式:

[
  {
    "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",
  }
]

我需要访问 "id" 元素,我在 .

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

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

这段代码给了我一个错误的数字,而且第二行收到了“不兼容的整数转换指针正在初始化……”的警告。 我知道 id 是一个保留关键字,但我需要访问这个元素。除了我没有实现服务器之外,我无法更改 id 元素名称。 您对如何访问它有什么建议吗?

将你的最后一行替换为:

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

顺便说一句,id 本身确实是 iOS 上的保留关键字,但是,您在字符串中使用它,因此它不会被编译器解释为关键字:) 所以, 没有理由在这里担心这个。