2012-06-28 48 views
1

我已經創建了一個屬性列表與鍵1至11中,所有與值0在屬性列表排序鍵在IOS

的列表要被用作數據源爲一選擇器視圖。

我有這些屬性:

@property (nonatomic, strong) NSDictionary *erfaring; 
@property (nonatomic, strong) NSArray *erfaringKeys; 

而這在實現文件:

NSString *erfaringFile = [[NSBundle mainBundle] 
         pathForResource:@"Erfaring" ofType:@"plist"]; 
erfaring = [[NSDictionary alloc] initWithContentsOfFile:erfaringFile]; 
erfaringKeys = [erfaring allKeys]; 

的問題是,當鑰匙到選擇器視圖加載它們進行排序錯誤的像:7,3,8,4,11,9,5,1,6,2,10

任何幫助?

回答

0

可以在picker-

erfaringKeys = [erfaring allKeys]; 
erfaringKeys = [erfaringKeys sortedArrayUsingSelector:@selector(compare:)]; 
+0

感謝排序裝貨前的陣列,但是當值的鍵被設置爲「數字」,它不工作。將它們設置爲 「字符串」 對它們進行分類1,10,11,2,3,4,5,6,7,8,9 –

+0

您可以檢查此線程解決這個問題 - http://stackoverflow.com/questions/ 4558761 /排序-AN-的NSArray-的-的NSString – rishi

1

您需要字典的

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: 
        @"String 1", @"1", 
        @"String 4", @"4", 
        @"String 3", @"3", 
        @"String 2", @"2", nil]; 

NSArray *sortedKeys = [dic keysSortedByValueUsingSelector:@selector(compare:)]; 

然後循環使用排序鍵

鍵排序
for (NSString *sortedKey in sortedKeys) { 
    NSString *value = [dic objectForKey:sortedKey]; 
}