Desmond
Desmond

Reputation: 5001

How do I retrieve an array from a dictionary which and then retrieve a dictionary within that array?

I would like to know how to retrieve an array from within a dictionary and then retrieve the dictionary from the array object.

i'm following this http://ios.biomsoft.com/2011/09/11/simple-xml-to-nsdictionary-converter/

itemDict 
 (
        {
        postID =         {
            text = 26;
        };
        score =         {
            text = 20;
        };
        title =         {
            text = "http://www.google.com quiz";
        };
        url =         {
            text = "http://www.google.com";
        };
    },
        {
        postID =         {
            text = 27;
        };
        score =         {
            text = 10;
        };
        title =         {
            text = "http://www.google.com";
        };
        url =         {
            text = "http://www.google.com";
        };
    },
        {
        postID =         {
            text = 3;
        };
        score =         {
            text = 41;
        };
        title =         {
            text = "http://www.google.com";
        };
        url =         {
            text = "http://www.google.com";
        };
    },
        {
        postID =         {
            text = 35;
        };
        score =         {
            text = 10;
        };
        title =         {
            text = "Sample Video";
        };
        url =         {
            text = "http://www.google.com";
        };
    },
        {
        postID =         {
            text = 43;
        };
        score =         {
            text = 30;
        };
        title =         {
            text = "sample";
        };
        url =         {
            text = "http://www.google.com";
        };
    }
)

Upvotes: 0

Views: 126

Answers (2)

alecnash
alecnash

Reputation: 1768

Lets suppose you parse it and have something like this:

    (
//object 0
    {
    postID= 26;
    score = 20;
    title = www.google.com
    url = www.google.com
    },
//object 1
    {
    postID= 27;
    score = 10;
    title = www.google.com
    url = www.google.com
    }...
    )

This is your array.

The objectAtIndex:0 which in this example is:

{
postID= 26;
score = 20;
title = www.google.com
url = www.google.com
}

is a dictionary.

Upvotes: 1

Sebastien Peek
Sebastien Peek

Reputation: 2528

Alrighty, what you want to do is the following.

From what I can gather from the link posted as well, you have a top level NSDictionary which then contains the XML inside it. There would be an object inside the NSDictionary which would, from your example, be itemDict but that, to my eyes, looks more like an NSArray.

From here, I'd store that NSArray inside another one.

Each object inside that array is an NSDictionary of keys and objects matched to them. Obviously I'm talking about this bit.

{
    postID =         {
        text = 26;
    };
    score =         {
        text = 20;
    };
    title =         {
        text = "http://www.google.com quiz";
    };
    url =         {
        text = "http://www.google.com";
    };
},

Maybe something like the following will let you dive down deep enough.

NSArray *itemArray = [xmlDictionary objectForKey:@"itemDict"];

for (NSDictionary *dict in itemArray) {

    NSLog(@"Dictionary: %@", dict);

}

That code should show the output of each NSDictionary inside the NSArray. From here it is a simple case of doing whatever you want with the NSDictionary.

EDIT

Just had another look and thought I'd add this in as well. It seems each NSDictionary we would make with the above code, then has a key for another object and that object is stored inside another dictionary. Something really weird with the XML if you ask me...

But, a work around could be the following.

NSArray *itemArray = [xmlDictionary objectForKey:@"itemDict"];

for (NSDictionary *dict in itemArray) {

    NSLog(@"Dictionary: %@", dict);

    for (NSDictionary *dictObject in [dict allKeys]) {

         NSLog(@"Dictionary Object: %@", dictObject);

    }

}

If this isn't what you're wanting to do, please edit your question so I can rectify my response.

Hope that helps!

Upvotes: 1

Related Questions