我需要循环一个包含两个键,一个对象和一个日期的Dictionary数组。我想循环一路同时进行检查。当找到相同的对象时,将会有一个计数增量。
一旦我将代码记录下来,就会显示出来:
value of severity (
{
Severity = Warning;
createdDate = "2015-12-09";
noOfOccurence = 1;
},
{
Severity = Informational;
createdDate = "2015-12-08";
noOfOccurence = 2;
},
{
Severity = Informational;
createdDate = "2015-12-08";
noOfOccurence = 1;
},
{
Severity = Warning;
createdDate = "2015-12-08";
noOfOccurence = 1;
},
{
Severity = Informational;
createdDate = "2015-12-05";
noOfOccurence = 1;
}
)
正如你在2015-12-08
期间所看到的,在索引1处有2个noOfOccurence。但在索引2处还有另一个2015-12-08
,其中有1个noOfOccurence。这是错误的,因为索引1和2处的此日期与2015-12-08
的总noOfOccurence应为3相同。
以下是我使用的代码:
dictionForSeverity = [[NSMutableDictionary alloc] init];
//code for same severity type for the same date
for (int i = 0; i < feeds.count; i++)
{
int count =1;
BOOL flag = false;
for (int j = i+1; j< feeds.count; j++)
{
if ([[feeds objectAtIndex:i] isEqualToDictionary:[feeds objectAtIndex:j]])
{
count++;
flag = true;
[feeds removeObjectAtIndex:j];
}
}
if (flag == true)
{
[dictionForSeverity setObject:[[feeds objectAtIndex:i] objectForKey:@"createdDate"] forKey:@"createdDate"];
[dictionForSeverity setObject:[[feeds objectAtIndex:i] objectForKey:@"Severity"] forKey:@"Severity"];
[dictionForSeverity setObject:[NSString stringWithFormat:@"%d",count] forKey:@"noOfOccurence"];
[arrFinalValues addObject:[dictionForSeverity copy]];
}
else
{
[dictionForSeverity setObject:[[feeds objectAtIndex:i] objectForKey:@"createdDate"] forKey:@"createdDate"];
[dictionForSeverity setObject:[[feeds objectAtIndex:i] objectForKey:@"Severity"] forKey:@"Severity"];
[dictionForSeverity setObject:[NSString stringWithFormat:@"%d",count] forKey:@"noOfOccurence"];
[arrFinalValues addObject:[dictionForSeverity copy]];
}
}
NSLog(@"value of severity %@",arrFinalValues);
//giving values according to date manage/replace object of an array according to date
for (int i=0; i<arrFinalValues.count; i++)
{
long indexOfObject;
NSLog(@"%@",[[arrFinalValues objectAtIndex:i] valueForKey:@"Severity"] );
if ( [[[arrFinalValues objectAtIndex:i] valueForKey:@"Severity"] isEqualToString:@"Informational"])
{
indexOfObject = [dateArrForXais indexOfObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"createdDate"]];
[arrInformation replaceObjectAtIndex:indexOfObject withObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"noOfOccurence"]];
}
else if ([[[arrFinalValues objectAtIndex:i] valueForKey:@"Severity"] isEqualToString:@"Warning"])
{
indexOfObject = [dateArrForXais indexOfObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"createdDate"]];
[arrWarning replaceObjectAtIndex:indexOfObject withObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"noOfOccurence"]];
}
else if ([[[arrFinalValues objectAtIndex:i] objectForKey:@"Severity"] isEqualToString:@"Critical"])
{
indexOfObject = [dateArrForXais indexOfObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"createdDate"]];
[arrCritical replaceObjectAtIndex:indexOfObject withObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"noOfOccurence"]];
}
else if ([[[arrFinalValues objectAtIndex:i] objectForKey:@"Severity"] isEqualToString:@"Emergency"])
{
indexOfObject = [dateArrForXais indexOfObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"createdDate"]];
[arrEmergency replaceObjectAtIndex:indexOfObject withObject:[[arrFinalValues objectAtIndex:i] valueForKey:@"noOfOccurence"]];
}
}
问:我想计算正确出现相同日期的相同对象的数量。请帮助我!谢谢。
答案 0 :(得分:0)
而不是所有这些,请使用包含四个项目的字典,全部为NSCountedSets:
这些countSets中的每一个都将包含日期和该日期的计数。当您添加一个集合成员或取出一个成员时,该计数将自动为您保留。
您要创建它的所有工作就是浏览您的Feed,然后获取严重性,并从字典中获取countsSet,然后将日期添加到字典中,然后转到下一个Feed条目,直到完成。
看起来像这样:
Dictionary:
key: "Warning" object: countedSet of dates
key: "Informational" object: countedSet of dates
key: "Emergency" object: countedSet of dates
key: "Critical" object: countedSet of dates
这应该允许您快速查找每个严重性级别,并查看出现这些错误的日期,以及每个日期的重复次数。
或者,您可以将日期用作键,将严重性用作设置项,这样您就可以查找日期并查看该日期的每条严重性消息的数量。这看起来像这样:
Dictionary:
key: 2015-12-05 object: countedSet of severity
key: 2015-12-07 object: countedSet of severity
key: 2015-12-08 object: countedSet of severity
key: 2015-12-08 object: countedSet of severity
每套只包含4个项目(警告,信息,紧急,严重),该套装将保留每个项目的数量。
答案 1 :(得分:0)
for (int i = 0; i < feeds.count; i++)
{
int count =1;
BOOL flag = false;
for (int j = i+1; j< feeds.count; j++)
{
//if same type of dictionary found multiple times then manage it's count and remove the same one as well
while ([[feeds objectAtIndex:i] isEqualToDictionary:[feeds objectAtIndex:j]])
{
count++;
flag = true;
//removing the same entry
[feeds removeObjectAtIndex:j];
}
}
NSMutableDictionary *dictionForClassification = [[NSMutableDictionary alloc] init];
//if flag is true mean same type of severity is present multiple time in same date then manage final array (to show on arrFinalValues) for graph
if (flag == true)
{
[dictionForClassification setObject:[[feeds objectAtIndex:i] objectForKey:@"createdDate"] forKey:@"createdDate"];
[dictionForClassification setObject:[[feeds objectAtIndex:i] objectForKey:@"Classification"] forKey:@"Classification"];
[dictionForClassification setObject:[NSString stringWithFormat:@"%d",count] forKey:@"noOfOccurence"];
[arrFinalValues addObject:[dictionForClassification copy]];
}
else
{
[dictionForClassification setObject:[[feeds objectAtIndex:i] objectForKey:@"createdDate"] forKey:@"createdDate"];
[dictionForClassification setObject:[[feeds objectAtIndex:i] objectForKey:@"Classification"] forKey:@"Classification"];
[dictionForClassification setObject:[NSString stringWithFormat:@"%d",count] forKey:@"noOfOccurence"];
[arrFinalValues addObject:[dictionForClassification copy]];
}
}
将If语句更改为While循环已经