ios - 如何将NSDictionary数据添加到NSMutableArray?

标签 ios objective-c nsmutablearray nsdictionary

我试图将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中使用“品牌”。

最佳答案

如果打印结果为空,则表示您没有创建“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);

关于ios - 如何将NSDictionary数据添加到NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33996318/

相关文章:

ios - 将 JSON 对象发布到 Restful Web 服务

iphone - 如果一个方法接受对象并添加到数组中,是否应在必要时检测并释放该对象?

ios - 禁用自动布局本地化行为(RTL - 从右到左行为)

iphone - NSMutableArray 正在删除具有相同字符串的所有对象

iOS NSMutableString 返回奇怪的字符

iphone - 配置门户上开发和分发配置文件的区别?

ios - iOS 9 中的 UIPickerView 最大宽度

ios - 在 Storyboard 中以模态序列传递数据?

ios - TableHeaderView 不响应触摸

objective-c - Swift:在 didSet 中通知委托(delegate)?