简体   繁体   中英

How to store an NSMutableArray to another NSMutableArray in Objective-C

Here is my JSON and source-code which I tried:
JSON:

{
    "total": 2,
    "floorplans": [
        {
            "_id": {
                "$id": "57fdd13e368e728c278328f6"
            },
            "indoor_map_name": “Building 1”,
            "geo_coords": {
                "lat": "42.0912000000000010",
                "long": "-71.2643390000000070"
            },
            "map_community_id": 20316,
            "map_drawing_id": 31728,
            "map_level_id": 53146,
            "organization": [
                "org-57d67a986edc6182458466"
            ],
            "drawings": [
                {
                    "map_drawing_id": "21419",
                    "levels": [
                        {
                            "map_level_id": "53136",
                            "min_height": "70",
                            "max_height": "75"
                        }
                    ]
                },
                {
                    "map_drawing_id": "31728",
                    "levels": [
                        {
                            "map_level_id": "53146",
                            "min_height": "70",
                            "max_height": "86"
                        },
                        {
                            "map_level_id": "53149",
                            "min_height": "86",
                            "max_height": "96"
                        },
                        {
                            "map_level_id": "53150",
                            "min_height": "96",
                            "max_height": "110"
                        }
                    ]
                }
            ],
            "uuid": "mapgroup-57fdd13e25afd890493911"
        }
    ]
}

Code:

-(void)getAllDrawingsandLevels:(NSDictionary *)dict
{
    int total = [[dict objectForKey:@"total"] intValue];
    if (total >0) {               
            arrFloorplans =[dict objectForKey:@"floorplans"];
            if (arrFloorplans!=nil) {
                for (int i=0; i<[arrFloorplans count]; i++) {
                    arrFloorplans1 = [arrFloorplans objectAtIndex:i];
                    //NSLog(@"arrDrawings1 %@",arrFloorplans1);

                NSString *srtCommID = [arrFloorplans1 valueForKey:@"map_community_id"];
                if ([[NSString stringWithFormat:@"%@",srtCommID] isEqualToString:[NSString stringWithFormat:@"%@",[kCommunityid objectAtIndex:i]]]) {
                    arrDrawings = [arrFloorplans1 valueForKey:@"drawings"];
                    if (arrDrawings !=nil) {
                        for (int j = 0; j<[arrDrawings count]; j++) {
                            arrDrawings1 = [arrDrawings objectAtIndex:j];
                            NSString *StrDrwaingID = [NSString stringWithFormat:@"%@",[arrDrawings1 valueForKey:@"map_drawing_id"]];
                            [arrDrawingsID addObject:StrDrwaingID];
                            //[arrLevelsID removeAllObjects];
                            arrLevels = [arrDrawings1 valueForKey:@"levels"];
                            for (int k = 0; k< [arrLevels count]; k++) {
                                arrLevels1 = [arrLevels objectAtIndex:k];
                                NSString *StrLevelID = [NSString stringWithFormat:@"%@",[arrLevels1 valueForKey:@"map_level_id"]];
                                [arrLevelsID addObject:StrLevelID];
                            }
        [arrFinalLevelID addObjectsFromArray:arrLevelsID];
                          }                           
                        NSLog(@"arrDrawingsID %@",arrDrawingsID);
                        NSLog(@"arrLevelsID %@",arrLevelsID);
                        NSLog(@"arrFinalLevelID %@",arrFinalLevelID);

                    }
                }
            }
    }
}  

I want Two arrays
1) arrDrawingsID = [21419][31728] which I got successfully and Second one

2) arrFinalLevelID = [53136][53146,53149,53150] But with this code my output is
[53136,53136,53146,53149,53150]

Thanks in Advance, Mihir

The problem is in this line [arrFinalLevelID addObjectsFromArray:arrLevelsID]; it should be [arrFinalLevelID addObject:arrLevelsID]; also you need to reinitialized arrLevelsID before loop.

arrLevelsID = [NSMutableArray alloc] init]; //reinitialized Array for removing previous value
arrLevels = [arrDrawings1 valueForKey:@"levels"];
for (int k = 0; k< [arrLevels count]; k++) {
    arrLevels1 = [arrLevels objectAtIndex:k];
    NSString *StrLevelID = [NSString stringWithFormat:@"%@",[arrLevels1 valueForKey:@"map_level_id"]];
    [arrLevelsID addObject:StrLevelID];
}
[arrFinalLevelID addObject:arrLevelsID]; //instead of addObjectsFromArray:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM