Objective c 如何在NSMutableArray中替换字典
这是我的JSON响应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:
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*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);
}