0

从字典中获取数据时出现以下错误

-[__NSCFString objectForKey:]: 无法识别的选择器发送到实例 0x4fbb80

这是我的代码

  NSDictionary *dic = [XMLReader dictionaryForXMLString:class.returnData error:nil];

  MyGiftsarray = [[dic objectForKey:@"response"] objectForKey:@"gifts"];


  for (NSDictionary *element in MyGiftsarray) {

  [MyGiftsnamearray addObject:[element objectForKey:@"name"]];

  [MyGiftsChkarray addObject:[element objectForKey:@"chk"]];

   [MyGiftsIdarray addObject:[element objectForKey:@"id"]];

   [MyGiftsApprovedarray addObject:[element objectForKey:@"approved"]];

   [MyGiftsisCompletedarray addObject:[element objectForKey:@"completed"]];

 }

这是我尝试从中获取数据的 NSDictionary。 MyGiftsDict->(

 {
    Appliedpoint = 500;
    Requiredpoint = 5000;
    approved = N;
    chk = 50;
    completed = 0;
    id = 66;
    name = "1-800Flowers.com";
},
    {
    Appliedpoint = 85;
    Requiredpoint = 2500;
    approved = N;
    chk = 25;
    completed = 0;
    id = 71;
    name = "Bath Body Works";
},
    {
    Appliedpoint = 5;
    Requiredpoint = 2500;
    approved = N;
    chk = 25;
    completed = 0;
    id = 75;
    name = "Buca di Beppo";
},
    {
    Appliedpoint = 36;
    Requiredpoint = 2500;
    approved = N;
    chk = 25;
    completed = 0;
    id = 66;
    name = "1-800Flowers.com";
},
    {
    Appliedpoint = 25;
    Requiredpoint = 5000;
    approved = N;
    chk = 50;
    completed = 0;
    id = 90;
    name = "Jelly Belly";
},
    {
    Appliedpoint = 120;
    Requiredpoint = 500;
    approved = N;
    chk = 5;
    completed = 0;
    id = 129;
    name = "Amazon.com";
}

)

4

3 回答 3

3

您以错误的方式迭代-使用-

MyGiftsDict = [[dic objectForKey:@"response"] objectForKey:@"gifts"];

然后逐步迭代字典。你需要使用类似的东西 -

for(NSString *key in [MyGiftsDict allKeys])
{
    [MyGiftsnamearray addObject:[MyGiftsDict objectForKey:key]];
}
于 2012-07-05T06:34:29.440 回答
0

Gifts 本身就是一个 NSDictionary。它不是一个 NSArray!你不需要那个 for 循环。简单使用

[MyGiftsNameArray addObject:[MyGiftsArray objectForKey:@"name"];

等等。

于 2012-07-05T06:34:56.167 回答
0

我认为您不需要for循环。

于 2012-07-05T06:35:57.937 回答