2012-10-03 105 views
0

可能重複:
Can i sort the NSDictionary on basis of key in Objective-C?訂購的NSDictionary allObjects通過按鍵

我有一個的.plist文件中的幾個字符串,我試圖返回到的NSDictionary,然後到一個數組,但返回的順序與我保存在播放列表中的順序不同。 到目前爲止,我已經嘗試

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
NSArray *strs = [NSArray arrayWithArray:[dict allKeys]]; 
strs = [strs sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

但這僅僅是返回鍵名。

+6

你不能訂購一個NSDictionary,它是一個固有的無序對象。您可以像您一樣對鍵進行排序,然後循環訪問有序鍵以按照該順序獲取值,但仍然不排序字典。 – rdelmar

+0

http://stackoverflow.com/questions/8829119/getting-values-as-in-order-they-added-with-nsdictionary – vikingosegundo

回答

0

不,你不能。儘管如此,你也可以用你的鑰匙保持一個有序數組。