哪个对象的数组具有大多数特定对象 [英] Which Object's Array Has Most of Particular Object

查看:78
本文介绍了哪个对象的数组具有大多数特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,其中包含许多信息,包括标题字符串,日期字符串和NSArray等.该对象具有8组不同的信息.我想找出哪个"Set的标题"出现最多的是"Sunday"一词.数据加载后,我进行一些计算:

I have an Object that contains many pieces of information, a Title String, Date String, and NSArray, amongst others. This Object has 8 different sets of this information. I want to find out which Set's Title has the most occurrences of the word "Sunday". After the data loads, I run some calculations:

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);

在控制台中,它为我提供了该对象的8组中每个NSArray中找到星期日的次数的打印输出.那么,我该如何最好地在出现次数最多的对象中获取Set的标题?

In Console, it gives me a printout of the number of times Sunday is found in each NSArray from that Object's 8 sets. How would I then best go about getting the title for the Set in the Object that contains the most occurrences?

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

Each set in the Object appears like this in the console:

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

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

Here's the entire Console Log for the 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并填充表的代码是:

The code I use to get the PFObject and populate table is:

- (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;
}

推荐答案

您已经编写了一个计算对象出现次数的函数.只是概括一下...

You've written a function that counts occurrances of an object. Just generalize it a bit...

- (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;
}

将要测试的对象的数组放入数组中,如下所示:

Put the object's arrays you want to test into an array, like this:

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

最大化功能的模式是设置一个不可能太低的最大值,并记录超出该最大值的参数...

The pattern to maximize a function is to set an impossibly low max, and record the parameter that exceeds it...

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);  

编辑,我建议您退出PFQueryTableVC进行此测试-并且,实际上,完全放弃它.最终,这是一个便利"类,您将无法使用.我现在知道这是一个开创性的起点,如果没有它,您可能还不知道该怎么做.

EDIT I'd advise getting out of the PFQueryTableVC for this test -- and, really, abandoning it altogether. It's a "convenience" class that will get in your way, ultimately. I understand for now that its a head-start, and you may not know enough yet to do without it.

无论如何,在viewWillAppear中,只需执行以下操作:

Anyway, in viewWillAppear, just do this:

[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);
}];

这篇关于哪个对象的数组具有大多数特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆