gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:15:58 24 4
gpt4 key购买 nike

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

{"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com