哪个 Object 的数组最特别 Object

Which Object's Array Has Most of Particular Object

我有一个 Object,其中包含许多信息、标题字符串、日期字符串和 NSArray,等等。这个 Object 有 8 组不同的信息。我想找出哪个 Set 的标题出现次数最多的单词 "Sunday"。数据加载后,我运行一些计算:

NSArray *yourArrayhere = self.theObject[@"DatesSuggested"];
    int occurrences = 0;
    for(NSString *string in yourArrayhere){
        occurrences += ([string isEqualToString:@"Sunday"]?1:0); //certain object is @"Sunday"
    }
    NSLog(@"number of occurences %d", occurrences);

在控制台中,它为我提供了从 Object 的 8 个集合中的每个 NSArray 中找到星期日的次数的打印输出。那么我怎样才能最好地获得 Object 中出现次数最多的集合的标题?

Object 中的每个设置在控制台中显示如下:

<Activities: 0x7fc3abe107a0, objectId: 6eUXy9SkZK, localId: (null)> {
    DatesSuggested =     (
        Sunday,
        Sunday
    );
    Title = Train;
    VotesAgainst = 0;
    VotesFor = 0;
}

这是 PFObject 的完整控制台日志:

2016-06-23 22:51:10.399 Roll 'Em Up[1675:150331] Bar D
2016-06-23 22:51:10.400 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a61f190, objectId: NZ1JNtDoIR, localId: (null)> {
    DatesSuggested =     (
    );
    Title = "Bar D";
    VotesAgainst = 0;
    VotesFor = 1;
}
2016-06-23 22:51:10.402 Roll 'Em Up[1675:150331] Creede Trip
2016-06-23 22:51:10.402 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a620650, objectId: VAu5qpGYLk, localId: (null)> {
    Title = "Creede Trip";
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.403 Roll 'Em Up[1675:150331] Durango Shopping
2016-06-23 22:51:10.403 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a620b10, objectId: sCKWlnI3Z7, localId: (null)> {
    Title = "Durango Shopping";
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.404 Roll 'Em Up[1675:150331] Fishing
2016-06-23 22:51:10.404 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a621310, objectId: Qy4zmA7jir, localId: (null)> {
    Title = Fishing;
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.405 Roll 'Em Up[1675:150331] Ouray Trip
2016-06-23 22:51:10.406 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a621880, objectId: Nmrw405JZo, localId: (null)> {
    Title = "Ouray Trip";
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.407 Roll 'Em Up[1675:150331] Pagosa Shopping
2016-06-23 22:51:10.407 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a621df0, objectId: ws0UbFwTtZ, localId: (null)> {
    Title = "Pagosa Shopping";
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.408 Roll 'Em Up[1675:150331] Rapids
2016-06-23 22:51:10.408 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a622300, objectId: UUxHmqYOM9, localId: (null)> {
    Title = Rapids;
    VotesAgainst = 0;
    VotesFor = 0;
}
2016-06-23 22:51:10.409 Roll 'Em Up[1675:150331] Train
2016-06-23 22:51:10.410 Roll 'Em Up[1675:150331] <Activities: 0x7fb33a622870, objectId: 6eUXy9SkZK, localId: (null)> {
    DatesSuggested =     (
    );
    Title = Train;
    VotesAgainst = 0;
    VotesFor = 0;
}

更新:

我用来获取 PFObject 并填充 table 的代码是:

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"Activities"];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if (self.objects.count == 0) {
        query.cachePolicy = kPFCachePolicyNetworkOnly;
    }

    [query orderByAscending:@"Title"];
    return query;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    self.theObject = object;

   //Code for the Cell Text removed for brevity

    [self performCalculations];
    return cell;
}

您编写了一个计算对象出现次数的函数。稍微概括一下...

- (NSInteger)occurrencesOf:(NSString *)string inArray:(NSArray *)array {
    NSInteger occurrences = 0;
    for(NSString *s in array){
        occurrences += ([s isEqualToString:string]?1:0);
    }
    NSLog(@"number of occurences %d", occurrences);
    return occurrences;
}

把你要测试的对象的数组放到一个数组中,像这样:

NSArray *arrays = @[object.array0, object.array1, ... .array8];

最大化函数的模式是设置一个不可能低的最大值,并记录超过它的参数...

NSArray *maxOccurrenceArray = nil; 
NSInteger maxOccurrences = -LONG_MAX;
for (NSArray *array in arrays) {
    NSInteger occurrences = [self occurrencesOf:@"Sunday" inArray:array];
    if (occurrences > maxOccurrences) {
        maxOccurrences = occurrences;
        maxOccurrenceArray = array;
    }
}
NSLog(@"The array with the most occurrences is %@", maxOccurrenceArray);  

EDIT 我建议在这个测试中退出 PFQueryTableVC —— 实际上,完全放弃它。 "convenience" class 最终会妨碍您。我现在明白这是一个先机,如果没有它,你可能还不够了解。

无论如何,在 viewWillAppear 中,只需这样做:

[super viewWillAppear:animated];

PFQuery *query = [self queryForTable];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    NSLog(@"this should be all of the objects %@", objects];
    // now just run the code I suggest, adapting objects as the array of arrays

    NSArray *maxOccurrenceArray = nil; 
    NSInteger maxOccurrences = -LONG_MAX;
    for (PFObject *pfObject in objects) {
        NSArray *array = [pfObject objectForKey:@"THE_NAME_OF_THE_ARRAY_PROPERTY"];
        NSInteger occurrences = [self occurrencesOf:@"Sunday" inArray:array];
        if (occurrences > maxOccurrences) {
            maxOccurrences = occurrences;
            maxOccurrenceArray = array;
       }
    }
    NSLog(@"The array with the most occurrences is %@", maxOccurrenceArray);
}];