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
섹션을 변경하고 섹션을 내림차순으로 정렬하려면 무엇을 변경해야합니까? 감사.
음 NSSortDescriptors를 사용하면 원하는 방식으로 모든 키를 정렬하는 것이 더 낫습니다. 귀하의 기준을 사용하여 그들을 정렬하고 주문 사전에서 개체를 얻을. – Sandeep
두 번째 방법이 효과가 있어야합니다. 두 가지 오류를 이해하지 못합니다. NSNumber인지 확인하기 위해 [rate class]를 기록하십시오. 당신이 생각하는 라인의 첫 번째 오류가 확실합니까? 두 번째 오류에 대해서는 caseInsensitiveCompare의 출처를 알 수 없습니다. 그거 어딘가에 놓았 니? 그 방법에 대한 선택기 그냥 비교했다 생각 : 그리고 NSNumber의 비교 버전을 사용해야합니다 : 당신이 그것을 NSNumber에 전달하면). – rdelmar