简体   繁体   English

如何将NSMutableArray存储到Objective-C中的另一个NSMutableArray

[英]How to store an NSMutableArray to another NSMutableArray in Objective-C

Here is my JSON and source-code which I tried: 这是我尝试过的JSON和源代码:
JSON: 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 1) arrDrawingsID = [21419][31728]我成功获得了第二个

2) arrFinalLevelID = [53136][53146,53149,53150] But with this code my output is 2) arrFinalLevelID = [53136][53146,53149,53150]但是使用此代码,我的输出是
[53136,53136,53146,53149,53150] [53136,53136,53146,53149,53150]

Thanks in Advance, Mihir 在此先感谢,Mihir

The problem is in this line [arrFinalLevelID addObjectsFromArray:arrLevelsID]; 问题出在这一行[arrFinalLevelID addObjectsFromArray:arrLevelsID]; it should be [arrFinalLevelID addObject:arrLevelsID]; 它应该是[arrFinalLevelID addObject:arrLevelsID]; also you need to reinitialized arrLevelsID before loop. 您还需要在循环之前重新初始化arrLevelsID

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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将NSMutableArray添加到NSMutableArray Objective-c - How to add an NSMutableArray to an NSMutableArray Objective-c 如何将我的客户NSMutableArray存储在Objective-C的NSUserDefaults中? - How to store my customer NSMutableArray in NSUserDefaults for Objective-C? 如何在NSMutableArray中对对象进行分组并在Objective-C中划分为单独的NSMutableArray - How to group object in NSMutableArray and divide to separate NSMutableArray in Objective-C objective-C 中的 NSMutableArray 插入 - NSMutableArray insertion in objective-C NSmutablearray是如何存储在NSmutableDictionary目标c中 - NSmutablearray is how to store in NSmutableDictionary objective c iOS / Objective-C:将NSString对象添加到NSMutableArray,NSMutableArray为(空) - iOS/Objective-C: Adding NSString object to NSMutableArray, NSMutableArray is (null) Objective-C 如何从其他地方添加对象到NSMutableArray? - Objective-C How to add objects to NSMutableArray from other place? 如何使用Objective-C使用两个NSMutableArray填充Expandable TableView - How to populate, Expandable TableView with two NSMutableArray using objective-c 如何在Objective-C中将NSMutableArray的元素设置为UILabel? - How to set the elements of NSMutableArray to UILabel in Objective-C? Objective-C:如何使用它们的标签对UIButton的NSMutableArray进行排序 - Objective-c: how to sort a NSMutableArray of UIButtons using their tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM