Resolved XML -> NSDictionary

Discussion in 'iOS Programming' started by DennisBlah, May 18, 2014.

  1. DennisBlah, May 18, 2014
    Last edited: May 19, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    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!
     
  2. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    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
     
  3. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #3
    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.
     
  4. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #4
    But with an NSLog to ouput the dictionary it loojs fine..


    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.
     
  5. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #5
    You are on the right track.
     
  6. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    Amazing. Then I also know how t make the user sort manually :D thanks ALOT!!!
     

Share This Page