Reputation: 363
Hi in one of my application,I have an array which contains a group of NSMutableDictionary
objects. The dictionary object have three key-value pairs as like below
And array having many number of objects. Here now by using different add buttons I am adding these dictionary objects to the array. Even while adding objects to array i am checking whether any duplicate objects are available or not using NSNotFound
method. As such below
if([Array indexOfObject:dicObject] == NSNotFound)
{
[Array addObject:dicObject];
}
Here it is working fine in few cases, But it's not working in other cases.I will explain with one example :
For example i have one dicobject in array with following key value pairs
company:XYZ Product:ABC Quantity:2
Now for example I want to add one more dic object with the same above key value pairs. That time obviously it won't add because already same product is available in array.
This is valid condition.
Exceptional Case: For example I want to add one more product with following values
Company:XYZ Product:ABC Quantity:6
At this case this product is adding into the array without any error. But my concern is i don't want to add this into the array again only the quantity have to update, because company and product name both are same so. So can you please show me the way to handle this scenario.
Upvotes: 2
Views: 1335
Reputation: 11006
You could use indexOfObjectPassingTest:
to know if a similar dictionary is already present in the array.
This may look something like this:
NSMutableArray *arr = // your array
NSDictionary *dicObject = // your object
NSUInteger indexOfDicObject = [arr indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return ([obj[@"company"] isEqualToString:dicObject[@"company"]] &&
[obj[@"product"] isEqualToString:dicObject[@"product"]]);
}];
if (indexOfDicObject == NSNotFound)
{
[arr addObject:dicObject];
}
else
{
NSNumber *quantity = arr[indexOfDicObject][@"quantity"];
arr[indexOfDicObject][@"quantity"] = @([quantity intValue] + [dicObject[@"quantity"] intValue]);
}
I made the following assumptions:
company
value is a NSString
;product
value is a NSString
;quantity
value is an integer, stored in a NSNumber
.See also trojanfoe's answer, which is better if you can replace your dictionaries by classes.
Upvotes: 5
Reputation: 122381
I think you need to change tack; first create a custom object to hold your company, product and quantity and ensure you implement the isEqual:
and hash
methods.
Then simply store your custom objects within an NSMutableSet
object, which will ensure that duplicates cannot exist.
Your custom object will now become your principle Model object for the app (i.e. provide the 'M' in MVC, the design pattern upon which Cocoa and Cocoa Touch apps are based) and you will find that it will be reused over and over as the app grows.
Upvotes: 3