plist does not order items !

Discussion in 'iOS Programming' started by jagatnibas, Sep 27, 2011.

  1. jagatnibas macrumors regular

    Joined:
    Jul 28, 2008
    #1
    I am trying to read a plist and write to a file in my iPhone application

    Code:
    NSString *finalPath = [path stringByAppendingPathComponent:@"myplist.plist"];
    	dict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
    	for (id key in dict) {
    		NSString *str = [NSString stringWithFormat:@"%@", key];
    		NSLog(@"bundle: key=%@, value=%@", key, [dict objectForKey:key]);
    	}
    
    here i am not getting items in the order they appear in the plist file

    why is it so ?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Arrays are ordered. Dictionaries are not. It's basically that simple. If you need to maintain ordering use a structure that is ordered.

    When you add an key/object pair to a dictionary the internal data structure of the hashmap will change. There is no reason to ever believe that this hash will retain the order that they keys were added.
     
  3. jagatnibas thread starter macrumors regular

    Joined:
    Jul 28, 2008
    #3
    is there a way to read the plist inan ordered manner ? any example piece of code ?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    The question is to broad. A plist can represent any arbitrary collection of data (more or less). If you want the data ordered use a dictionary in the plist. That is my advice. I do not post code for others to copy without understanding as that leads to them not learning anything. You have now been told the solution twice.
     
  5. astrorider macrumors 6502

    Joined:
    Sep 25, 2008
    #5
    If I understand the discussion you should use an array(not a dictionary) in the plist which does maintain order, then read the array from the plist.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    You can get the allKeys array from the plist dictionary, sort it, and read your items in order.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    In the order you sort the keys yes, but this may not be the order the items are in the plist. If the plist happens to have the items in alphabetic order that's great but what if, for example, they are stations on a train line?
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Right. It depends. If the data represents an ordered list then an array of dictionaries would be a normal way of doing things. If there were some other reason to have a dictionary of dictionaries they could still be placed in an arbitrary sorted order, once read into memory, by adding an index property to each dictionary and sorting on that.
     

Share This Page