在围绕数组和词典进行游戏时,我正在解决iPhone中的一个小问题。我有和以下数据的产品词典
name = Product;
options = (
{
code = code1;
name = "product AAA";
},
{
code = code1;
name = "product BBB";
},
{
code = "code2";
name = "product BBB";
},
{
code = "code3";
name = "product CCC";
},
{
code = "code3";
name = "product DDD";
},
{
code = code4;
name = "product EEE";
},
{
code = code4;
name = "product FFF";
}
);
我也有一系列匹配产品
matchingProducts
{
"product BBB",
"product CCC",
"product DDD"
)
现在,我想要做的就是我想从productsProducts数组中删除产品字典。我该怎么做。 注意:根据我的业务规则,我无法使用密钥删除对象。我有问题,因为我有重复名称,但我必须得到最终结果字典,如下所示。可能吗。
name = Product;
options = (
{
code = code1;
name = "product AAA";
},
{
code = code1;
name = "product BBB";
},
{
code = code4;
name = "product EEE";
},
{
code = code4;
name = "product FFF";
}
);
如果我的问题不明确,请回复我。
我使用下面的代码修复了java中的类似问题
for (int j = 0; j < matchingProducts.size(); j++) {
String product = ((Product) matchingProducts.elementAt(i)).name;
for (int i = 0; i <Product.size(); i++) {
String productName = ((Product) Product.elementAt(i)).name;
if (product.equals(productName)) {
Product.removeElementAt(i);
break;
}
}
}
答案 0 :(得分:1)
NSMutableDictionary *Product = initialize like you did with Products;
NSMutableDictionary *resultant = [NSMutableDictionary alloc]init]; //this will have your result
NSArray *keys = [Product allKeys];
for(int i = 0 ; i < [keys count]; i++)
{
id temp = [Product objectForKey:keys[i]];
bool matchFound = NO;
for (int j = 0; j < [matchingProduct count]; j++)
{
id temptemp = [matchingProduct objectAtIndex:j] //Assuming matchingProduct is an NSArray or NSMutableArray
if(temp == temptemp)
{
matchFound = YES;
}
}
if(!matchFound)
[resultant addObject:temp];
}
//结果现在有你想要的。使用它。