0

我的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 票。另一个数组是相同的,但显示星期四最受欢迎。我很困惑为什么它会这样。

4

0 回答 0