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

jagatnibas

macrumors regular
Original poster
Jul 28, 2008
126
0
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 ?
 
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.
 
is there a way to read the plist inan ordered manner ? any example piece of code ?
 
is there a way to read the plist inan ordered manner ? any example piece of code ?

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.
 
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.

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.
 
You can get the allKeys array from the plist dictionary, sort it, and read your items in order.

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?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.