ios - 循环遍历嵌套的 NSMutableDictionary 并过滤一些项目

标签 ios objective-c loops nested-loops nsmutabledictionary

我已经被困在这里一段时间了,尝试了很多事情都没有成功。 所以我有一个看起来像这样的嵌套字典:

{"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": []}}}

我需要做的是遍历此处的所有项目以访问键/值对 time,如果时间值的值介于 0 - 90 之间,我想保存它。

这里重要的是结构没有改变。我的意思是说,例如我们对所有项目进行了迭代并检查了时间值,那么新字典看起来像:

{"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;
        }
    }];
}];

关于ios - 循环遍历嵌套的 NSMutableDictionary 并过滤一些项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743121/

相关文章:

javascript - PhoneGap应用程序浏览器内: Cannot open link with "%" character

ios - 一部 Iphone 6 与另一部的布局差异

ios - 在 UIScrollView 上显示大型 UIView 的最佳方式是什么?

ios - 如何创建多行 UITextfield?

iphone - 为什么在显示 UIAlertView 时应用会崩溃?

iphone - Apple/iPhone 开发 IDE 首选项?

iphone - 如何从照片库加载照片并将其存储到应用程序项目中?

c++ - 如何在循环外设置初始值? C++

java - 求二维数组中某一列的平均值并将结果放入另一个矩阵中

c - gcc:为简单循环生成的奇怪 asm