Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

I'm making an NSDictionary from an XML file.
But I'm having trouble going through the dictionary.

The xml generator
Code:
echo '<companies>';
for($i=0;$i<3;$i++) {
echo '<company' . $i . '><name>Company' . $i . '</name><description>Test copmany' . $i . '!</description><coords>51.12312,004.123123</coords></company' . $i . '>';
}
echo '</companies>';

Thats how I generate XML. That goes fine.

This is the result in NSDictionary:
Code:
{
    companies =     {
        company0 =         {
            coords =             {
                text = "51.12312,004.123123";
            };
            description =             {
                text = "Test copmany0!";
            };
            name =             {
                text = Company0;
            };
        };
        company1 =         {
            coords =             {
                text = "51.12312,004.123123";
            };
            description =             {
                text = "Test copmany1!";
            };
            name =             {
                text = Company1;
            };
        };
        company2 =         {
            coords =             {
                text = "51.12312,004.123123";
            };
            description =             {
                text = "Test copmany2!";
            };
            name =             {
                text = Company2;
            };
        };
    };
}

This part pretty ok..

What is the best way to loop through all the 'company' records, and so make a simple list like..
Company0 : Name = Company0 : Description = Test Company0
Company1 : Name = Company1 : Description = Test Company1
Company2 : Name = Company2 : Description = Test Company2

I'd like to understand this... 'multi-dimensional arrays'. But not sure how to call to it, and googling doesnt make much sense.

Thanks!
 
Last edited:
Ok I kinda figured it out.
This works:
Code:
NSDictionary theList = ....................
for (id tempObject in theList) {
        NSLog(@"Name: %@", [[[theList objectForKey:tempObject] objectForKey: @"name"] objectForKey: @"text"]);
    }
(object text contains the 'text' within the object for 'name')

Only the order of it is not correct:

2014-05-19 20:24:43.938 myApp[6377:60b] Name: Company4
2014-05-19 20:24:43.939 myApp[6377:60b] Name: Company0
2014-05-19 20:24:43.939 myApp[6377:60b] Name: Company3
2014-05-19 20:24:43.939 myApp[6377:60b] Name: Company2
2014-05-19 20:24:43.939 myApp[6377:60b] Name: Company5
2014-05-19 20:24:43.939 myApp[6377:60b] Name: Company1

However the dictionary is in order Company0 to Company5
 
However the dictionary is in order Company0 to Company5

No it's not because a dictionary is an unordered collection.

You could keep your dictionary keys in an array which is an ordered collection. Note that the array will not replace your dictionary but rather supplement it. I'll let you make a go at figuring out how to accomplish it.
 
No it's not because a dictionary is an unordered collection.
But with an NSLog to ouput the dictionary it loojs fine..


You could keep your dictionary keys in an array which is an ordered collection. Note that the array will not replace your dictionary but rather supplement it. I'll let you make a go at figuring out how to accomplish it.

Hmm sounds difficult. I know how to make an array or an mutable one to edit values later. But I dont fully understand what you mean. Store the keys to the array and sort the array. Loop through it and then based on that order get the values i want? That shouldnt be that hard. If Im right with this thought.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.