2012-07-02 8 views
0

IOS의 propertylist에서 키를 정렬하는 데 어려움이 있습니다.키 정렬 값

나는 내가 metode로 정렬 배열 키를 추출하는 것을에서 사전에있는 목록을로드하고 "sortedArrayUsingSelector'selector는 (비교 :"

을 내가 키를 사용하려고하면, 그 값은 거기에 열쇠를 줘서 정렬되지 않은 것 같습니다. exsample에 (키 진값) 정렬되지 않은 : 아 센스 0 덴스 1 Nyborg, 덴마크 1 미델 파르트 0 분류 : 아 센스, 1, 덴 1, 0 Nyborg, 덴마크, 미델 파르트 0

`

NSBundle *bundle = [NSBundle mainBundle]; 
    NSURL *KSplistURL = [bundle URLForResource:@"KommunerStedtillæg" 
           withExtension:@"plist"]; 
    NSDictionary *dictionary = [NSDictionary 
           dictionaryWithContentsOfURL:KSplistURL]; 
    self.kommuner = dictionary; 

    //NSArray *keys = [[self.kommuner allKeys] sortedArrayUsingSelector:@selector(compare)]; 

    NSArray *components = [self.kommuner allKeys]; 
    NSArray *keys = [components sortedArrayUsingSelector:@selector(compare:)]; 


    NSMutableDictionary *newDict = [NSMutableDictionary dictionary]; 
    for (id key in keys)[newDict setObject:[dictionary objectForKey:key]forKey:key]; 




    self.kommunerKeys = [newDict allKeys]; 
    self.kommunerValues = [newDict allValues]; 

`

+0

코드 스 니펫을 게시하십시오! – DPlusV

답변

2

왜 사전이 관련없는 키 배열의 복사본을 정렬한다는 사실을 알고 있다고 생각합니까?

NSArray *keys = [[originalDict allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
NSMutableDictionary *newDict = [NSMutableDictionary dictionary]; 
for (id key in keys) 
    [newDict setObject:[originalDict objectForKey:key] forKey:key]; 

지금 newDict는 정렬 키 값을 포함합니다 :이 같은 다른 사전을 만들어야합니다.

+0

고마워하지만 지금은 응용 프로그램이 추락했습니다 ... 이 라인을 설명해 주시겠습니까? (키의 id 키)에 대한 [newDict setObject : [dictionary objectForKey : key] forKey : key]; –

+0

명확하지 않은 경우 앱을 개발하지 않아야합니다 ... 동일한 순서로 객체를 추가하는 모든 정렬 된 키를 반복하는 for 루프입니다. Btw 정확한 오류 메시지를 게시하지 않는 한, 나는별로 도움이되지 못한다. –

+0

나는 som 줄을 주석으로 처리하려고했는데 문제는 배열 키를 만드는 것으로 보인다. –