0

In my project I have a NSMutableArray * arrayMatchs with dictionaries as objects. In this dictionary I have a Key with the string as value that coincide between a dictionaries in array. There is a way to create a new array with inside array as objects filled with dictionary with the same key / value?

Below my first array(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" = "";
}

)

The new array that i need to fill UITableView should be composed by arrays,in the specific case, with one dictionary for the first object and two dictionary for second object.

At this point number of section = newArray count

number of rows in section = [newArray object at index:section]count

I hope everything is clear and thanks for all the help

Alex1982

1 Answer 1

1

Its not efficient or pretty, but the idea is to loop your array, building a new one as you go. For each item in the original, you want to know if an array containing that id already exists in the new array. If it does, add it there. If it doesn't, add it (within a new mutable array) to the answer. Like this...

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I'll try your solution and at the same time ill try to find another way to get the same result. At the moment,cause my lack experience with objective c, i cannot think of anything else. Almost certainly i believe that the feed could be structured in a better way to make programmer's life more easy. Thanks for the help and if I ill find something better will share solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.