如何将NSDictionary数据添加到NSMutable数组?

时间:2015-11-30 10:25:23

标签: ios objective-c nsmutablearray nsdictionary

我正在尝试将aDict数据添加到myData(NSMutable Array),但无法添加它。每当我试图打印它(myData)。它打印为null。

for ( NSDictionary *aDict in responseArray){
    NSLog(@"%@", aDict[@"brand"]);
    myData[count]=aDict[@"brand"];
 }
 NSLog(@"%@",myData[2]);

我的数据格式为

{
"data": [
    {
        "brand": "AMANTE",
        "brand_id": 216,
        "size": "XL",
        "size_id": 78,
        "url_path": "brands-1.html?brand=216&size=78"
    },
    {
        "brand": "PENNY",
        "brand_id": 1349,
        "size": "LARGE",
        "size_id": 54,
        "url_path": "brands-1.html?brand=1349&size=54"
    },
    {
        "brand": "TRIUMPH",
        "brand_id": 58,
        "size": "LARGE",
        "size_id": 54,
        "url_path": "brands-1.html?brand=58&size=54"
    },
    {
        "brand": "BENETTON",
        "brand_id": 2519,
        "size": "LARGE",
        "size_id": 54,
        "url_path": "brands-1.html?brand=2519&size=54"
    },
    {
        "brand": "HANES",
        "brand_id": 446,
        "size": "LARGE",
        "size_id": 54,
        "url_path": "brands-1.html?brand=446&size=54"
    },
    {
        "brand": "JOCKEY",
        "brand_id": 60,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=60&size=76"
    },
    {
        "brand": "LOVABLE",
        "brand_id": 59,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=59&size=76"
    },
    {
        "brand": "HANES",
        "brand_id": 446,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=446&size=76"
    },
    {
        "brand": "ENAMOR",
        "brand_id": 56,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=56&size=76"
    },
    {
        "brand": "BIARA",
        "brand_id": 2480,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=2480&size=76"
    },
    {
        "brand": "PENNY",
        "brand_id": 1349,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=1349&size=76"
    },
    {
        "brand": "TRIUMPH",
        "brand_id": 58,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=58&size=76"
    },
    {
        "brand": "LIBERTINA",
        "brand_id": 4167,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=4167&size=76"
    },
    {
        "brand": "INNER SENSE",
        "brand_id": 3685,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=3685&size=76"
    },
    {
        "brand": "ANITA",
        "brand_id": 3919,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=3919&size=76"
    },
    {
        "brand": "ADIRA",
        "brand_id": 2584,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=2584&size=76"
    },
    {
        "brand": "COUCOU",
        "brand_id": 2008,
        "size": "40B",
        "size_id": 76,
        "url_path": "brands-1.html?brand=2008&size=76"
    },
    {
        "brand": "362436",
        "brand_id": 2119,
        "size": "XL",
        "size_id": 78,
        "url_path": "brands-1.html?brand=2119&size=78"
    }
],
"status": "success"
}

我只想"品牌"在我的NSMutable Array * myData。

3 个答案:

答案 0 :(得分:1)

尝试:如果你的字典数组包含键data,你可以直接访问:

NSMutableArray *addarra = [NSMutableArray new];

for ( NSDictionary *aDict in responseArray){
   NSMutableDictionary *temp = [NSMutableDictionary new];
  [temp setObject:aDict[@"brand"] forKey:@"brand"];
   [addarra addobject:temp];

}

提取:

NSString *brand  = [[addarra objectAtIndex:i]ObjectForKey:@"brand"];

答案 1 :(得分:1)

如果打印结果为空,则表示您没有创建" mydata" NSMutableArray里。这是我的代码。

NSDictionary *dict = @{@"data":@[
                                   @{
                                       @"brand": @"AsafdsadfsdANTE",
                                       @"brand_id": @216,
                                       @"size": @"XL",
                                       @"size_id": @78,
                                       @"url_path": @"brands-1.html?brand=216&size=78"
                                       },@{
                                       @"brand": @"UYTIT",
                                       @"brand_id": @216,
                                       @"size": @"XL",
                                       @"size_id": @78,
                                       @"url_path": @"brands-1.html?brand=216&size=78"
                                       },@{
                                       @"brand": @"sFASDSADF",
                                       @"brand_id": @216,
                                       @"size": @"XL",
                                       @"size_id": @78,
                                       @"url_path": @"brands-1.html?brand=216&size=78"
                                       },@{
                                       @"brand": @"GERWW",
                                       @"brand_id": @216,
                                       @"size": @"XL",
                                       @"size_id": @78,
                                       @"url_path": @"brands-1.html?brand=216&size=78"
                                       },

                                   ],
                           @"status":@"success"

                           };
    NSMutableArray *myData = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSArray *data = dict[@"data"];
    for (NSDictionary *dict in data) {
        NSString *brand = dict[@"brand"];
        NSLog(@"%@",brand);
        [myData addObject:brand];
    }
    NSLog(@"%@",myData);

答案 2 :(得分:1)

brand

的列表

最简单的方法是:

NSArray *_brands = [responseArray[@"data"] valueForKeyPath:@"brand"];

将为您提供一个包含所有值的数组,但在最终集合中可以多次使用一个品牌,因此,例如TRIUMPH个品牌在原始数据源中是两倍,在_brands数组中也是两倍。

brand个值仅

你可以这样做:

NSArray *_brands = [[NSSet setWithArray:[responseArray[@"data"] valueForKeyPath:@"brand"]] allObjects];

甚至是:

NSArray *_brands = [responseArray[@"data"] valueForKeyPath:@"@distinctUnionOfObjects.brand"];

在这两种情况下,每个品牌只会在_brands数组中出现一次,例如TRIUMPH,虽然它出现两次,但在输出集合中只会出现一次,无论数据源具有相同值的次数是多少;你可以使用你更喜欢的任何一个或者对你的项目更有意义。

可变集合

如果你最后需要一个可变的集合:

NSMutableArray *_mutableBrands = [_brands mutableCopy];