Frage

I am reading a plist file using code below. My problem is that the file seems to be read in some random order. The order is always the same (d,b,e,c,a,f). Is it possible to read the file in the "correct" order, as entered in file?

prefs.plist file content:

<dict>
    <key>a</key>
    <string>1</string>
    <key>b</key>
    <string>2</string>
    <key>c</key>
    <string>3</string>
    <key>d</key>
    <string>4</string>
    <key>e</key>
    <string>5</string>
    <key>f</key>
    <string>6</string>
</dict>

Code:

//Get file name
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"prefs" ofType:@"plist"];

//Load preferences into dictionary
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

Printout of myDict:

<CFBasicHash 0x4e1f330 [0xe39400]>{type = mutable dict, count = 6,
entries =>
    0 : <CFString 0x4e1f120 [0xe39400]>{contents = "d"} = <CFString 0x4e1f180 [0xe39400]>{contents = "4"}
    1 : <CFString 0x4e1f160 [0xe39400]>{contents = "b"} = <CFString 0x4e1f320 [0xe39400]>{contents = "2"}
    3 : <CFString 0x4e1f130 [0xe39400]>{contents = "e"} = <CFString 0x4e1f190 [0xe39400]>{contents = "5"}
    4 : <CFString 0x4e1f110 [0xe39400]>{contents = "c"} = <CFString 0x4e1f170 [0xe39400]>{contents = "3"}
    5 : <CFString 0x4e1f140 [0xe39400]>{contents = "a"} = <CFString 0x4e1f1a0 [0xe39400]>{contents = "1"}
    6 : <CFString 0x4e1f150 [0xe39400]>{contents = "f"} = <CFString 0x4e1f310 [0xe39400]>{contents = "6"}
}
War es hilfreich?

Lösung

That's not necessarily a problem with initWithContentsOfFile:.

If that's your intent and your dictionary, simply create an ordered array, like so:

NSArray * keys = [myDict keys]; 
NSArray * orderedKeys =
 [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

of course, you could use an compare or define your own to order them.

Then you can enumerate through the ordered keys like an array.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top