Which Object's Array Has Most of Particular Ob

2019-09-04 23:15发布

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

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

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

UPDATE:

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

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-04 23:25

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

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.

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);
}];
查看更多
登录 后发表回答