Search code examples
iosobjective-cloopsnested-loopsnsmutabledictionary

Looping through a nested NSMutableDictionary and filtering some items


I have been stuck here for a while trying many things without any success. So i have a nested dictionary that looks like this:

{"Div 3 Mellersta G\u00f6taland, herrar": {
    "0": {"awayScore": "0", "homeTeam": "Hestrafors IF", "tempTime": "12:00", "homeScore": "0", "awayTeam": "Gunnilse IS", "time": "90'", "events": []}, 
    "1": {"awayScore": "0", "homeTeam": "Lerums IS", "tempTime": "13:00", "homeScore": "0", "awayTeam": "Skara FC", "time": "44'", "events": []}, 
    "2": {"awayScore": "0", "homeTeam": "Vara SK ", "tempTime": "14:00", "homeScore": "0", "awayTeam": "IFK Falk\u00f6ping FF", "time": "14:00", "events": []}, 
    "3": {"awayScore": "0", "homeTeam": "Holmalunds IF Alings\u00e5s", "tempTime": "15:00", "homeScore": "0", "awayTeam": "Lunden \u00d6ver\u00e5s BK", "time": "15:00", "events": []}, 
    "4": {"awayScore": "0", "homeTeam": "IFK Tidaholm", "tempTime": "16:00", "homeScore": "0", "awayTeam": "S\u00e4vedalens IF", "time": "16:00", "events": []}}, 
"Div 4 A Herrar": {
    "0": {"awayScore": "0", "homeTeam": "SKIF Semberija", "tempTime": "17:00", "homeScore": "0", "awayTeam": "Floda BoIF", "time": "17:00", "events": []}, 
    "1": {"awayScore": "0", "homeTeam": "Partille IF FK", "tempTime": "18:00", "homeScore": "0", "awayTeam": "Kode IF", "time": "18:00", "events": []}, 
    "2": {"awayScore": "0", "homeTeam": "IK Kongah\u00e4lla", "tempTime": "19:00", "homeScore": "0", "awayTeam": "Romelanda UF", "time": "19:00", "events": []}}}

What i need to do is to iterate through all the items here to access the key/value pair time and if the time value has a value from 0 - 90 i want to save it.

What is important here is that the structure is not changed. By that i mean lets say for example we made an iteration over all the items and checked the time value, then the NEW dictionary would look like:

{"Div 3 Mellersta G\u00f6taland, herrar": {
    "0": {"awayScore": "0", "homeTeam": "Hestrafors IF", "tempTime": "12:00", "homeScore": "0", "awayTeam": "Gunnilse IS", "time": "90'", "events": []}, 
    "1": {"awayScore": "0", "homeTeam": "Lerums IS", "tempTime": "13:00", "homeScore": "0", "awayTeam": "Skara FC", "time": "44'", "events": []}, 
}}

all the other items are filtered away since the first two items in Div 3 Mellersta was the only ones with the time value of 0 - 90.

Note it should be written in Objective-C

Thank you all!


Solution

  • Try this code

    NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
    [rootDict enumerateKeysAndObjectsUsingBlock:^(NSString *key1, NSDictionary *dict1, BOOL *stop) {
        [dict1 enumerateKeysAndObjectsUsingBlock:^(NSString *key2, NSDictionary *dict2, BOOL *stop) {
            NSString *time = dict2[@"time"];
            if([time integerValue] >= 0 && [time integerValue] <= 90 && ![time containsString:@":"]) {
                if(!newDict[key1]) {
                    newDict[key1] = [NSMutableDictionary dictionary];
                }
                newDict[key1][key2] = dict2;
            }
        }];
    }];