我有一个NSArray
与NSMutableDictionaries
。示例testArray =[dict1,dict2,dict3,dict4].
数组中的每个字典都类似于
dict1= {
country = "INDIA";
flag = "A";
Currency = "Rupees";
rate = 10;
}
所有值都可以更改。有时候
dict1 = {
country = "USA";
flag = "SA";
Currency = "Dollar";
rate = 50;
}
如果country = "INDIA"
和flag = "A"
以及Currency = "Rupees"
并且不需要更改费率键的值,我需要更新testArray中所有词典的关键费率值testArray旁边的其他词典。
答案 0 :(得分:3)
试试下面的内容。
for(NSMutableDictionary *dic in array)
{
if([[dic valueForKey:@"country"] isEqualToString:@"INDIA"] &&
[[dic valueForKey:@"flag"] isEqualToString:@"A"] &&
[[dic valueForKey:@"Currency"] isEqualToString:@"Rupees"])
{
[dic setValue:@"100" forKey:@"rate"];
}
}
答案 1 :(得分:0)
以下是您需要的完整示例
NSDictionary *dict1 = @{
@"country" : @"INDIA",
@"flag" : @"A",
@"Currency" : @"Rupees",
@"rate" : @10
};
NSDictionary *dict2 = @{
@"country" : @"USA",
@"flag" : @"SA",
@"Currency" : @"Dollar",
@"rate" : @50
};
NSArray *testArray = @[dict1, dict2];
NSMutableArray *testMutableArray = [testArray mutableCopy];
for (int i = 0; i < testMutableArray.count; i++) {
NSDictionary *country = testMutableArray[i];
if ([country[@"country"] isEqualToString:@"INDIA"] &&
[country[@"flag"] isEqualToString:@"A"] &&
[country[@"Currency"] isEqualToString:@"Rupees"])
{
NSMutableDictionary *countryMutable = [country mutableCopy];
countryMutable[@"rate"] = @100;
testMutableArray[i] = [countryMutable copy];
}
}
testArray = [testMutableArray copy];
答案 2 :(得分:0)
使用NSMutableArray而不是NSArray。
然后,
NSDictionary *dict = [testArray objectAtIndex:0];
dict[@"rate"] = @"11";
[testArray replaceObjectAtIndex:0 withObject:dict];
希望这会对你有所帮助。