mardi 8 décembre 2015

IOS how to loop correctly while performing checks

I Need to loop a Dictionary array which contains two keys, one object and one date. I want to loop all the way to do a check at the same time. When objects are found the same there will be an increment of count.

Once i log it out the codes it shows this:

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;
    }
)

As you can see during 2015-12-08 there are 2 noOfOccurence at index 1. But at index 2 there are also another 2015-12-08 that has 1 noOfOccurence. It is wrong as this date at index 1 and 2 are the same the total noOfOccurence for 2015-12-08 should be 3.

Here is the code that i use:

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: i want to count the number of identical objects that occurs the same date correctly. Please assist me! Thank You.

Aucun commentaire:

Enregistrer un commentaire