Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在NSMutableArray中替换字典_Objective C - Fatal编程技术网

Objective c 如何在NSMutableArray中替换字典

Objective c 如何在NSMutableArray中替换字典,objective-c,Objective C,这是我的JSON响应 data = { Apparels = ( { "category_id" = 1650002; "category_name" = Apparels; "country_id" = 1; "created_by" = 1; "created_on" = "2016-03-17 13:

这是我的JSON响应

data =     {
    Apparels =         (
                    {
            "category_id" = 1650002;
            "category_name" = Apparels;
            "country_id" = 1;
            "created_by" = 1;
            "created_on" = "2016-03-17 13:29:00";
            id = 2;
            "is_checked" = 0;
            "is_delete" = 1;
            "modified_by" = 1;
            "modified_on" = "2016-08-26 05:53:21";
            "short_name" = UP;
            status = 550002;
            value = UP;
        },
                    {
            "category_id" = 1650002;
            "category_name" = Apparels;
            "country_id" = 1;
            "created_by" = 1;
            "created_on" = "2016-03-17 13:29:00";
            id = 3;
            "is_checked" = 0;
            "is_delete" = 1;
            "modified_by" = 1;
            "modified_on" = "2016-08-26 10:41:12";
            "short_name" = "<null>";
            status = 550001;
            value = MP;
        },
                    {
            "category_id" = 1650002;
            "category_name" = Apparels;
            "country_id" = 1;
            "created_by" = 4;
            "created_on" = "2016-04-08 12:10:17";
            id = 4;
            "is_checked" = 0;
            "is_delete" = 1;
            "modified_by" = 1;
            "modified_on" = "2017-03-06 10:35:04";
            "short_name" = HR;
            status = 550002;
            value = Haryana;
        },
                    {
            "category_id" = 1650002;
            "category_name" = Apparels;
            "country_id" = 1;
            "created_by" = 4;
            "created_on" = "2016-04-08 12:10:36";
            id = 5;
            "is_checked" = 0;
            "is_delete" = 1;
            "modified_by" = 1;
            "modified_on" = "2016-08-26 05:53:29";
            "short_name" = HP;
            status = 550002;
            value = "Himachal Pardesh";
        }
    );
};
NSLog返回以下内容:

    (
        {
        "category_id" = UP;
        "is_checked" = 1;
        value = UP;
    },
        {
        "category_id" = 1650002;
        "category_name" = Apparels;
        "country_id" = 1;
        "created_by" = 1;
        "created_on" = "2016-03-17 13:29:00";
        id = 3;
        "is_checked" = 0;
        "is_delete" = 1;
        "modified_by" = 1;
        "modified_on" = "2016-08-26 10:41:12";
        "short_name" = "<null>";
        status = 550001;
        value = MP;
    },
        {
        "category_id" = 1650002;
        "category_name" = Apparels;
        "country_id" = 1;
        "created_by" = 4;
        "created_on" = "2016-04-08 12:10:17";
        id = 4;
        "is_checked" = 0;
        "is_delete" = 1;
        "modified_by" = 1;
        "modified_on" = "2017-03-06 10:35:04";
        "short_name" = HR;
        status = 550002;
        value = Haryana;
    },
        {
        "category_id" = 1650002;
        "category_name" = Apparels;
        "country_id" = 1;
        "created_by" = 4;
        "created_on" = "2016-04-08 12:10:36";
        id = 5;
        "is_checked" = 0;
        "is_delete" = 1;
        "modified_by" = 1;
        "modified_on" = "2016-08-26 05:53:29";
        "short_name" = HP;
        status = 550002;
        value = "Himachal Pardesh";
    }
)
(
{
“类别识别号”=向上;
“是否选中”=1;
价值=上升;
},
{
“类别识别号”=1650002;
“类别名称”=服装;
“国家/地区id”=1;
“创建单位”=1;
“创建时间”=“2016-03-17 13:29:00”;
id=3;
“是否选中”=0;
“is_delete”=1;
“修改为”=1;
“修改日期”=“2016-08-26 10:41:12”;
“短名称”=“”;
状态=550001;
数值=MP;
},
{
“类别识别号”=1650002;
“类别名称”=服装;
“国家/地区id”=1;
“创建单位”=4;
“创建时间”=“2016-04-08 12:10:17”;
id=4;
“是否选中”=0;
“is_delete”=1;
“修改为”=1;
“修改日期”=“2017-03-06 10:35:04”;
“短名称”=人力资源;
状态=550002;
价值=哈里亚纳;
},
{
“类别识别号”=1650002;
“类别名称”=服装;
“国家/地区id”=1;
“创建单位”=4;
“创建时间”=“2016-04-08 12:10:36”;
id=5;
“是否选中”=0;
“is_delete”=1;
“修改为”=1;
“修改日期”=“2016-08-2605:53:29”;
“短名称”=马力;
状态=550002;
value=“Himachal Pardesh”;
}
)
在此之后,它在numberOfRowsInSection崩溃


任何帮助都将不胜感激。提前感谢。

此代码的结果是NSDictionary,但您创建了一个数组对象:

响应指令[@“数据”];

您可以与服务器讨论,数据必须是一个集合,NSArray的格式,现在它是一个NSDictionary

您有一些错误首先解决它,错误是

  • 您可以在复选框按钮集标记中进行单元设计

    [cell.checkBtn setTag:indexPath.row]

  • 管理复选框按钮的检测点击操作

    UIButton*按钮=(UIButton*)发送方;
    int row=button.tag

  • 使用此行对象管理替换对象

    首先,获取所选对象并将其存储在一个NSMutableDictionary对象中

    NSMutableDictionary*dic=[detailArr objectAtIndex:row]

    B.将对象替换为设定值

    [dic设置值:@“1”forKey:@“已检查”]

    C.此替换值仅更改“dic”对象,而不是您选择的单元格,因此下面的列表是数组列表中的更改对象

    [detailArr replaceObjectAtIndex:row with object:dic]

    最后一个是在tableview中


  • 问题是因为“衣服”中的数组是不可变的。我终于找到了解决办法

        detailArr = [[NSMutableArray alloc]init];
        if([responseDict[@"data"] count] > 0){
            keys = [responseDict[@"data"] allKeys];
            NSMutableDictionary *dict1;
            NSMutableArray *arrx;
            for(int i=0;i<[responseDict[@"data"] count];i++){
                arrx=[[NSMutableArray alloc]init];
                arrx = [NSMutableArray arrayWithArray:[responseDict[@"data"] valueForKey:[keys objectAtIndex:i] ]];
                dict1=[[NSMutableDictionary alloc]init];
                [dict1 setObject:arrx forKey:[keys objectAtIndex:i]];
               [detailArr addObject:dict1];
              }
    
         }
       [_tblVw reloadData];
    

    坠机后日志。
        detailArr = [[NSMutableArray alloc]init];
        if([responseDict[@"data"] count] > 0){
            keys = [responseDict[@"data"] allKeys];
            NSMutableDictionary *dict1;
            NSMutableArray *arrx;
            for(int i=0;i<[responseDict[@"data"] count];i++){
                arrx=[[NSMutableArray alloc]init];
                arrx = [NSMutableArray arrayWithArray:[responseDict[@"data"] valueForKey:[keys objectAtIndex:i] ]];
                dict1=[[NSMutableDictionary alloc]init];
                [dict1 setObject:arrx forKey:[keys objectAtIndex:i]];
               [detailArr addObject:dict1];
              }
    
         }
       [_tblVw reloadData];
    
    - (void)actionCheck:(UIButton*)sender{
          UIButton *btn = (UIButton*)sender;
          tagvalue = [ValidationClass getRowByBtn:btn :_tblVw];
          sectionValue = [ValidationClass getSectionByBtn:btn :_tblVw];
    
          NSMutableDictionary *replaceDict = [[NSMutableDictionary alloc]init];
          if([[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"is_checked"] objectAtIndex:tagvalue] isEqual:@"1"]){
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"category_id"] objectAtIndex:tagvalue]forKey:@"category_id"];
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"id"] objectAtIndex:tagvalue]forKey:@"id"];
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"];
             [replaceDict setObject:@"0" forKey:@"is_checked"];
             [[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] replaceObjectAtIndex:tagvalue withObject:replaceDict];
             NSLog(@"%@",detailArr);
    
             [_tblVw reloadData];
             [self addProduct:detailArr andPosition:tagvalue];
        }else{
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"category_id"] objectAtIndex:tagvalue]forKey:@"category_id"];
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"id"] objectAtIndex:tagvalue]forKey:@"id"];
             [replaceDict setObject:[[[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] valueForKey:@"value"] objectAtIndex:tagvalue] forKey:@"value"];
    
             [replaceDict setObject:@"1" forKey:@"is_checked"];
             [[[detailArr valueForKey:[keys objectAtIndex:sectionValue]] objectAtIndex:sectionValue] replaceObjectAtIndex:tagvalue withObject:replaceDict];
    
            NSLog(@"%@",detailArr);
            [self addProduct:detailArr andPosition:tagvalue];
        }
    
            NSLog(@"%@", detailArr);
    
     }