我需要循環包含兩個鍵,一個對象和一個日期的Dictionary數組。我想循環一路做同時檢查。當發現對象相同時,會有一個計數增量。IOS如何在執行檢查時正確循環
一旦我登錄它的代碼就說明這一點:
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
看到有2 noOfOccurence索引1但在指數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"]];
}
}
QNS:我想數數正確發生相同日期的相同對象。請幫助我!謝謝。
它已經解決了。它發生了我使用if語句。一旦我改變到While循環它的工作 – zac