我有一个包含许多信息的对象,标题字符串,日期字符串和NSArray等。该对象有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);
在Console中,它给出了一个打印输出,表示在每个NSArray中从该对象的8组中找到星期日的次数。那么我最好如何获得包含最多出现次数的对象中的Set的标题?
对象中的每个集合在控制台中都显示如下:
<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和populate表的代码是:
- (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;
}
答案 0 :(得分:1)
您已经编写了一个计算对象出现次数的函数。请稍微概括一下......
- (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);
编辑我建议退出PFQueryTableVC进行此测试 - 实际上,完全放弃它。这是一个方便&#34;最终将妨碍你的课程。我现在明白这是一个先发制人,如果没有它,你可能还不知道。
无论如何,在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);
}];