2012-11-14 4 views
0
 self.sections = [[NSMutableDictionary alloc] init]; 
     BOOL found; 
     for (NSDictionary *wine in sortedWines) 
     { 
      NSNumber *rate = [wine valueForKey:@"Rate"]; 
      NSString *rateStr = [NSString stringWithFormat:@"%.f", [rate floatValue]]; 
      found = NO; 
      for (NSString *str in [self.sections allKeys]) 
      { 
       if ([str isEqualToString:rateStr]) 
       { 
        found = YES; 
       } 
      } 
      if (!found) 
      {[self.sections setValue:[[NSMutableArray alloc] init] forKey:rateStr];} 
     } 
     for (NSDictionary *wine in sortedWines) 
     {[[self.sections objectForKey:[NSString stringWithFormat:@"%.f", [[wine valueForKey:@"Rate"] floatValue]] ] addObject:wine];} 

     // Sort: 
     for (NSString *key in [self.sections allKeys]) 
     {[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]]];} 

이 코드는 내 와인을 섹션에 배치하지만 내림차순으로 정렬하지 않습니다! NSNumber가 NSString으로 변환 되었기 때문일 수 있습니까? 나는의 NSNumber 값을 사용하여 코드를 만들려고했습니다섹션을 내림차순으로 정렬하려고 시도합니다.

 self.sections = [[NSMutableDictionary alloc] init]; 
     BOOL found; 
     for (NSDictionary *wine in sortedWines) 
     { 
      NSNumber *rate = [wine valueForKey:@"Rate"]; 
      found = NO; 
      for (NSNumber *str in [self.sections allKeys]) 
      { 
       if ([str isEqualToNumber:rate]) 
       { 
        found = YES; 
       } 
      } 
      if (!found) 
      {[self.sections setValue:[[NSMutableArray alloc] init] forKey:rate];} 
     } 
     // Loop again to sort wines into their keys 
     for (NSDictionary *wine in sortedWines) 
     {[[self.sections objectForKey:[wine valueForKey:@"Rate"]] addObject:wine];} 

     // Sort each section array 
     for (NSString *key in [self.sections allKeys]) 
     {[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]]];} 

을하지만 말한다

if (!found) 
{[self.sections setValue:[[NSMutableArray alloc] init] forKey:rate];} 

에 대한 경고를 표시합니다 "의 NSNumber 유형있는 NSString의 매개 변수에 ___strong 전송 호환되지 않는 포인터 타입"

앱을 실행하면 오류가 발생하여 오류가 발생합니다. -[__NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x1e085810

섹션을 변경하고 섹션을 내림차순으로 정렬하려면 무엇을 변경해야합니까? 감사.

+0

음 NSSortDescriptors를 사용하면 원하는 방식으로 모든 키를 정렬하는 것이 더 낫습니다. 귀하의 기준을 사용하여 그들을 정렬하고 주문 사전에서 개체를 얻을. – Sandeep

+1

두 번째 방법이 효과가 있어야합니다. 두 가지 오류를 이해하지 못합니다. NSNumber인지 확인하기 위해 [rate class]를 기록하십시오. 당신이 생각하는 라인의 첫 번째 오류가 확실합니까? 두 번째 오류에 대해서는 caseInsensitiveCompare의 출처를 알 수 없습니다. 그거 어딘가에 놓았 니? 그 방법에 대한 선택기 그냥 비교했다 생각 : 그리고 NSNumber의 비교 버전을 사용해야합니다 : 당신이 그것을 NSNumber에 전달하면). – rdelmar

답변

2

sortDescriptorWithKey : ascending :에 대한 기본 선택기가 이제 caseInsensitiveCompare :인지 나는 잘 모르겠다. 어쨌든 sortDescriptorWithKey : ascending : selector :를 대신 사용할 수 있으며 selector :에 compare :를 전달할 수 있습니다. 두 번째 오류가 수정되어야한다고 생각합니다. 여전히 첫 번째 오류가 발생하는 이유를 아직 모릅니다.

1

가독성을 위해 코드의 형식을 지정하면 훨씬 잘 이해할 수 있습니다. 예 : 중지하고 section 배열을 덤프 수 있기 때문에 무엇보다도

// Loop again to sort wines into their keys 
    for (NSDictionary *wine in sortedWines) { 
     NSArray* section = [self.sections objectForKey:[wine valueForKey:@"Rate"]]; 
     [section addObject:wine]; 
    } 

    // Sort each section array 
    NSArray* sortDescriptorArray = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Rate" ascending:NO]]; 
    for (NSString *key in [self.sections allKeys]) { 
     NSArray* section = [self.sections objectForKey:key]; 
     [section sortUsingDescriptors:sortDescriptorArray]; 
    } 

이 훨씬 간단 디버깅을합니다.

또는 정말로 다른 방법으로 더 좋아한다면 APL 또는 LISP를 대신 배우는 것이 좋습니다.

관련 문제