Issue in iPhone Dictionary?
I am revolving around with a small issue in iPhone while playing around Array and Dictionary. I have and product dictionary with the following data
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";
}
);
Also i have an array of matching products
matchingProducts
{
"product BBB",
"product CCC",
"product DDD"
)
Now, all i want to do is i want to remove from products dictionary comparing to matchingProducts array. how can i do it. Note: I cannot use key to remove objects as per my business rules. I have issue as i have names are repeated but i have to get the final result dictionary as shown below. Is it possible.
name = Product;
options = (
{
code = code1;
name = "product AAA";
},
{
code = code1;
name = "product BBB";
},
{
code = code4;
name = "product EEE";
},
{
code = code4;
name = "product FFF";
}
);
Please, reply me back if my question is not clear.
I have fixed the similar issue in java using the below code
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;
}
}
}
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];
}
// resultant now has what you wanted. Use it.
精彩评论