gpt4 book ai didi

ios - 如何创建一个包含键值对的子NSArray?

转载 作者:行者123 更新时间:2023-12-01 18:16:38 26 4
gpt4 key购买 nike

我有以下NsArray,我想对其进行排序并创建一个包含特定键值对的子数组。

{
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
},
{
category = 3;
"league_code" = ita;
"team_away" = Torino;
"team_home" = Milan;
},
{
category = 3;
"league_code" = ita;
"team_away" = Lazio;
"team_home" = Juve;
}

}
}

并且我想创建一个新数组或只修改数组以仅包含“league_code” = epl键的对象。

所以我想得到
    {
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
}

}
}

我试图将其转换为使用dictionaryWithObjects:forKeys,但是没有用。

最佳答案

NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[@"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[@"league_code"];
if ([leagueCode isEqualToString @"epl"]) {
[newArray addObject:dictEntry];
}
}

那样难吗?

关于ios - 如何创建一个包含键值对的子NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21370428/

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