我使用Parse.com并使用PFObject来填充TableView。 PFObject包含几个NSStrings和一个NSArray。该数组将包含一周中的几天,其中一些会多次出现。我只需要一种方法来确定哪个对象在该数组中每周的每一天都有最多。找到最受欢迎的一个,我取得了一些成功,但它给出了数组标题的数字。
[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);
}];
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);