正如我在 Adam Eberbach 回答的评论中所说,我使用NSDictionary
( self.workOrdersByDate
, 下面) 来存储分组结果,以及NSArray
( self.dateSections
, 下面) 来存储字典的排序键。
// Fetch all the WorkOrders
NSArray *results = [WorkOrder findAll];
// Filter based on isComplete property
NSPredicate *filter;
if (self.segmentedStatus.selectedSegmentIndex == SegmentAssigned) {
filter = [NSPredicate predicateWithFormat:@"isComplete == 0"];
} else {
filter = [NSPredicate predicateWithFormat:@"isComplete == 1"];
}
results = [results filteredArrayUsingPredicate:filter];
// Retain an NSArray of sorted NSDate keys
self.dateSections = [[results valueForKeyPath:@"@distinctUnionOfObjects.scheduledStartDate.beginningOfDay"] sortedArrayUsingSelector:@selector(compare:)];
// Create the dictionary
self.workOrdersByDate = [[NSMutableDictionary alloc] initWithCapacity:self.dateSections.count];
for (int sectionNum = 0; sectionNum < self.dateSections.count; sectionNum++) {
NSPredicate *sectionPredicate = [NSPredicate predicateWithFormat:@"scheduledStartDate.beginningOfDay == %@", [self.dateSections objectAtIndex:sectionNum]];
NSArray *workOrdersForSection = [results filteredArrayUsingPredicate:sectionPredicate];
[resultsDict setObject:workOrdersForSection forKey:[self.dateSections objectAtIndex:sectionNum]];
}