循环遍历嵌套的 NSMutableDictionary 并过滤一些项目
Looping through a nested NSMutableDictionary and filtering some items
我被困在这里已经有一段时间了,尝试了很多东西都没有成功。
所以我有一个嵌套字典,看起来像这样:
{"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": []}}}
我需要做的是遍历此处的所有项目以访问 key/value 对 time 并且如果时间值的值为 0 - 90我想保存它。
这里重要的是结构没有改变。我的意思是说,例如我们对所有项目进行了迭代并检查了时间值,那么 NEW 字典将如下所示:
{"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": []},
}}
所有其他项目都被过滤掉,因为 Div 3 Mellersta 中的前两个项目是唯一时间值为 0 - 90 的项目。
注意应该写成Objective-C
谢谢大家!
试试这个代码
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;
}
}];
}];
我被困在这里已经有一段时间了,尝试了很多东西都没有成功。 所以我有一个嵌套字典,看起来像这样:
{"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": []}}}
我需要做的是遍历此处的所有项目以访问 key/value 对 time 并且如果时间值的值为 0 - 90我想保存它。
这里重要的是结构没有改变。我的意思是说,例如我们对所有项目进行了迭代并检查了时间值,那么 NEW 字典将如下所示:
{"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": []},
}}
所有其他项目都被过滤掉,因为 Div 3 Mellersta 中的前两个项目是唯一时间值为 0 - 90 的项目。
注意应该写成Objective-C
谢谢大家!
试试这个代码
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;
}
}];
}];