freelancer
freelancer

Reputation: 1658

How to get data into NSArray from NSDictionary

i am getting following error while getting data from dictionary

"-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x4fbb80"

here is my code

  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"]];

 }

this is NSDictionary from which i try to get data. 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";
}

)

Upvotes: 0

Views: 3143

Answers (3)

Rui Peres
Rui Peres

Reputation: 25927

I don't think you will need the forcycle.

Upvotes: 0

user529758
user529758

Reputation:

Gifts is an NSDictionary in itself. It's not an NSArray! You don't need that for loop. Use simply

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

et cetera.

Upvotes: 0

rishi
rishi

Reputation: 11839

You are iterating in wrong manner- Use -

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

And then iterate over dictionary step by step. You need to use something like -

for(NSString *key in [MyGiftsDict allKeys])
{
    [MyGiftsnamearray addObject:[MyGiftsDict objectForKey:key]];
}

Upvotes: 3

Related Questions