如何将 NSDictionary 数据添加到 NSMutableArray?

Posted

技术标签:

【中文标题】如何将 NSDictionary 数据添加到 NSMutableArray?【英文标题】:how to add NSDictionary data to NSMutable Array? 【发布时间】:2015-11-30 10:25:23 【问题描述】:

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

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 数组 *myData 中的“品牌”。

【问题讨论】:

for (NSDictionary *aDict in responseDictionary["data"]) ... ,我猜你有一个根对象,你只是忽略它。 【参考方案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】:

brand 值列表

最简单的方法是:

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

这将为您提供一个包含所有值的数组,但一个 brand 可以在最终集合中多次出现,所以如果例如TRIUMPHbrands 在您的原始数据源中是两次,在_brands 数组中也是两次。


每个brand 只赋值一次

你可以这样做:

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

甚至那个:

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

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


可变集合

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

NSMutableArray *_mutableBrands = [_brands mutableCopy];

【讨论】:

【参考方案3】:

试一下:如果你的字典数组包含键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"];

【讨论】:

以上是关于如何将 NSDictionary 数据添加到 NSMutableArray?的主要内容,如果未能解决你的问题,请参考以下文章

如何在序列化为 json 之前将 NSDictionary 添加到另一个 NSDictionary

PHP json_encode() 与空 NSDictionary

如何将字典的 NSDictionary 添加到 NSArray?

Obj-C:将对象数组转换为 NSDictionary 以与 JSONRepresentation 一起使用

嵌套的 JSON 数据将导致使用 NS 字典(在 Swift 中)崩溃

如何在 iOS 中使用 NSDictionary 添加图像并使用 POST 方法将数组列表发送到服务器