2015-12-09 23 views
0

我需要循環包含兩個鍵,一個對象和一個日期的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:我想數數正確發生相同日期的相同對象。請幫助我!謝謝。

回答

0

代替這一切,使用具有四個項目,全部NSCountedSets的字典:

  • 一個countedSet爲 「警告」。
  • A計數設置爲「信息」。
  • 計數設置爲「緊急」。
  • 計數設置爲「關鍵」。

這些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個項目(警告,信息,緊急和嚴重)和設定將保持每個計數。

+0

它已經解決了。它發生了我使用if語句。一旦我改變到While循環它的工作 – zac

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循環已經可以