내 응용 프로그램에 NSDictionaries의 NSArray가 있습니다. 각 사전에는 "RunDate"라는 NSDate가 있습니다. 지금 내가 겪고있는 문제는 그것을하기 위해 노력하고있는 코드가 매우 비효율적이라는 것입니다. 기본적으로 모든 사전에서 날짜 당 단 하나의 섹션 만 원합니다. 그런 다음 각 섹션 (해당 날짜별로 정렬)에서 해당 날짜의 적절한 사전을로드합니다.NSDictionary에서 NSDate로 UITableView 섹션을로드 하시겠습니까?
아래 코드에서 NSDictionary의 새 NSArray를 만들었습니다.이 NSDictionary에는 날짜와 날짜가 포함되어 있으므로 각 섹션의 행 수를 알 수 있습니다. 문제는이 코드가 매우 비효율적이며 느껴지고 코드가 올바르지 않거나 개선 될 수있는 방법이 있는지 궁금합니다. 500 개 이상의 항목이있을 수 있으며 지금 가지고있는 코드는 매우 느립니다. 누구든지 그것에 대한 제안이 있습니까?
runArray = [[NSMutableArray alloc] init];
runArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"RunArray"] mutableCopy];
runDictTableArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in runArray) {
NSDictionary *runInfoDict = [[NSMutableDictionary alloc] init];
NSDate *theDate = [dict objectForKey:@"RunDate"];
//Check if we already have this date in the saved run array
BOOL goToNextDict = FALSE;
for (NSDictionary *savedDict in runDictTableArray) {
if ([theDate compare:[savedDict objectForKey:@"RunDate"]] == NSOrderedSame) {
goToNextDict = TRUE;
break;
}
}
if (goToNextDict)
continue;
////////////////////////////
//Now we check how many more we have of this date
int numbOfDate = 1;
int index = (int)[runArray indexOfObject:dict];
for (int i = index; i < [runArray count]; i++) {
NSDictionary *dictInner = [runArray objectAtIndex:i];
if ([theDate compare:[dictInner objectForKey:@"RunDate"]] == NSOrderedSame) {
numbOfDate++;
}
}
////////////////////////////
[runInfoDict setValue:[NSNumber numberWithInt:numbOfDate] forKey:@"DateAmount"];
[runInfoDict setValue:theDate forKey:@"Date"];
[runDictTableArray addObject:runInfoDict];
}
NSSortDescriptor를 사용해 보셨나요? 아마도 그렇게 쉽게 될 것입니다 ... – Echizzle