在我的项目中,我有一个NSMutableArray * arrayMatchs,其中字典作为对象。 在这本词典中,我有一个Key,字符串作为值,在数组中的字典之间重合。 有一种方法可以创建一个内部数组的新数组作为填充了具有相同键/值的字典的对象吗?
在我的第一个数组(arrayMatchs)下面:
arrayMatchs (
{
"match_commentary_available" = facup;
"match_comp_id" = 1198; <------------------------
"match_date" = "Sep 09";
"match_et_score" = "";
"match_formatted_date" = "09.09.2014";
"match_ft_score" = "";
"match_ht_score" = "";
"match_id" = 1937555;
"match_localteam_id" = 19941;
"match_localteam_name" = "Hartley Wintney";
"match_localteam_score" = "?";
"match_season_beta" = "";
"match_static_id" = 1844143;
"match_status" = "18:30";
"match_time" = "18:30";
"match_timer" = "";
"match_venue_beta" = "";
"match_venue_city_beta" = "";
"match_venue_id_beta" = 0;
"match_visitorteam_id" = 20839;
"match_visitorteam_name" = Ardley;
"match_visitorteam_score" = "?";
"match_week_beta" = "";
},
{
"match_commentary_available" = "spain_cup";
"match_comp_id" = 1397; <-------------------------------
"match_date" = "Sep 09";
"match_et_score" = "";
"match_formatted_date" = "09.09.2014";
"match_ft_score" = "";
"match_ht_score" = "";
"match_id" = 1909702;
"match_localteam_id" = 16021;
"match_localteam_name" = Girona;
"match_localteam_score" = "?";
"match_season_beta" = "";
"match_static_id" = 1845426;
"match_status" = "18:00";
"match_time" = "18:00";
"match_timer" = "";
"match_venue_beta" = "";
"match_venue_city_beta" = "";
"match_venue_id_beta" = 0;
"match_visitorteam_id" = 16184;
"match_visitorteam_name" = Tenerife;
"match_visitorteam_score" = "?";
"match_week_beta" = "";
},
{
"match_commentary_available" = "spain_cup";
"match_comp_id" = 1397; <--------------------------------
"match_date" = "Sep 09";
"match_et_score" = "";
"match_formatted_date" = "09.09.2014";
"match_ft_score" = "";
"match_ht_score" = "";
"match_id" = 1909694;
"match_localteam_id" = 15997;
"match_localteam_name" = Alaves;
"match_localteam_score" = "?";
"match_season_beta" = "";
"match_static_id" = 1845427;
"match_status" = "20:00";
"match_time" = "20:00";
"match_timer" = "";
"match_venue_beta" = "";
"match_venue_city_beta" = "";
"match_venue_id_beta" = 0;
"match_visitorteam_id" = 16079;
"match_visitorteam_name" = Osasuna;
"match_visitorteam_score" = "?";
"match_week_beta" = "";
}
)
我需要填充UITableView的新数组应该由数组组成,在特定情况下,第一个对象有一个字典,第二个对象有两个字典。
此时段数= newArray计数
section中的行数= [index:section的newArray对象] count
我希望一切都清楚,并感谢所有的帮助
Alex1982
答案 0 :(得分:1)
它不高效或漂亮,但我们的想法是循环你的阵列,随时建立一个新阵列。对于原始文件中的每个项目,您想知道新数组中是否已存在包含该ID的数组。如果是,请将其添加到那里。如果没有,则将其(在新的可变数组中)添加到答案中。像这样......
NSMutableArray *groupedArray = [NSMutableArray array];
for (NSDictionary *d in arrayMatchs) {
// see if there's a mathing dictionary in the new array
NSMutableArray *matchingInnerArray;
for (NSMutableArray *innerArray in groupedArray) {
NSDictionary *firstDictionary = innerArray[0];
NSNumber *innerId = firstDictionary[@"match_comp_id"];
if ([innerId isEqualToNumber:d[@"match_comp_id"]]) {
matchingInnerArray = innerArray;
break;
}
}
if (!matchingInnerArray) {
matchingInnerArray = [NSMutableArray array];
[groupedArray addObject:matchingInnerArray];
}
[matchingInnerArray addObject:d];
}
NSLog(@"%@", groupedArray);