我需要对NSArray
内的PFObject
进行排序。 PFObject
包含多个项目,包括一个数组,其中包含一周中的几天,我需要知道PFObject内每个项目最常重复的日期。在我的cellForRowAtIndexPath
内(因为我需要知道TableView中的每个项目,我有这个:
NSArray *yourArrayhere = object[@"DatesSuggested"];
if (yourArrayhere == NULL) {
//cell.text = @"No votes have been cast for this activity yet.";
}
else {
NSCountedSet *setOfObjects = [[NSCountedSet alloc] initWithArray:yourArrayhere];
//Declaration of objects
NSString *mostOccurringObject = @"";
NSUInteger highestCount = 0;
//Iterate in set to find highest count for a object
for (NSString *strObject in setOfObjects)
{
NSUInteger tempCount = [setOfObjects countForObject:strObject];
highestCount = tempCount;
mostOccurringObject = strObject;
NSLog(@"MOSTO%@", mostOccurringObject);
NSLog(@"MOSTC%lu", (unsigned long)highestCount);
}
NSString *allcombined = [[[[[[[@"Currently there are " stringByAppendingString:votesFor] stringByAppendingString:@" votes for and "] stringByAppendingString:votesAgainst] stringByAppendingString:@" votes against "] stringByAppendingString:object[@"Title"]] stringByAppendingString:@", with the most suggested date being "] stringByAppendingString:mostOccurringObject];
cell.detailTextLabel.text = allcombined;
NSString *toSave = [mostOccurringObject stringByAppendingString:[NSString stringWithFormat:@" %lu", (unsigned long)highestCount]];
NSLog(@"Highest %@", toSave);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:toSave forKey:cell.textLabel.text];
[defaults synchronize];
}
这在某些时候有用。其中一个数组中有(Monday, Monday, Monday, Thursday)
,它显示周一最受欢迎,有3票。另一个阵列是相同的,但显示星期四最受欢迎。我很难理解为什么这样做。