在NSArray中计算重复的对象仅在某些时间工作

时间:2016-06-28 00:31:33

标签: ios objective-c arrays nsarray nscountedset

我需要对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票。另一个阵列是相同的,但显示星期四最受欢迎。我很难理解为什么这样做。

0 个答案:

没有答案