我想用其他值替换我的数组的内容,
这是我的数组值:
[Arr objectAtIndex:i] = "End_Date" = "31 Mar 2014";
"Merchant_Category_Id" = 162;
"Merchant_ID" = 1262;
"Merchant_Name" = Mp;
"Merchant_SmallImage" = "http://abcdm/imge/cb256.jpg";
"Voucher_Id" = 130510;
"Voucher_Name" = "Rs 555 OFF";
favStatus = 0;
我想要做的是,我想在0
1
更新从i
到[Arr replaceObjectAtIndex:[[[Arr objectAtIndex:i] objectForKey:@"favStatus"] integerValue] withObject:@"1"];
的favStatus
这是我尝试过的代码片段,
0
每当我记录[[[Arr objectAtIndex:i] objectForKey:@"favStatus"] integerValue]
请提前帮助和谢谢。
答案 0 :(得分:1)
您正在使用字符串@“0”替换整个对象(字典)。
实际上你可以创建一个新的tempDictionary,并提取@“favStatus”的keyValue。替换它,然后将新对象放在数组中:
NSMutableDictionary *tempDictionary = [[Arr objectAtIndex:i] mutableCopy];
[tempDictionary setValue:@"0" forKey:@"favStatus"];
[Arr replaceObjectAtIndex: i
withObject: tempDictionary];
答案 1 :(得分:1)
试试这段代码。
NSMutableDictionary *yourDictionary = [[Arr objectAtIndex:i] mutableCopy];
[yourDictionary removeObjectForKey:@"favStatus"];
[yourDictionary setValue:@"0" forKey:@"favStatus"];
[Arr replaceObjectAtIndex: i
withObject: yourDictionary];
我希望这段代码对你有用。
答案 2 :(得分:1)
NSMutableDictionary *tempDictionary = [[Arr objectAtIndex:0] mutableCopy];
[tempDictionary setValue:@"1" forKey:@"favStatus"];
[Arr replaceObjectAtIndex:0 withObject:tempDictionary];
答案 3 :(得分:0)
这看起来很奇怪。数组的元素是NSMutableDictionaries
吗?如果是,您可以使用favStatus
更新i
索引arr[i][@"favStatus"] = @1;
。
答案 4 :(得分:0)
你可以试试这个。我以它为例
使用数组并添加词典。
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int i=0;i<4;i++){
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:@"karun%d",i] forKey:@"Merchant_Name"];
[dict setValue:[NSNumber numberWithInt:0] forKey:@"favStatus"];
[arr addObject:dict];
}
NSLog(@"Before Replace : %@",arr.description);
现在我要将所有字典favStatus值替换为1
for(int i=0;i<arr.count;i++){
NSMutableDictionary *dict = (NSMutableDictionary*)[arr objectAtIndex:i];
[dict setValue:[NSNumber numberWithInt:1] forKey:@"favStatus"];
}
NSLog(@"After Replace : %@",arr.description);
希望这会有所帮助:)