1

I have one array that is pre-filled with dictionaries with each dictionary consisting of two keys.

{
   name = object1;
   quantity = 5;
},
{ 
   name = object2;
   quantity = 2;
}

I want to be able to add more objects and combine any duplicate dictionaries. So for example, if I added 5 of object one and 3 of object 2, this would be the result.

 {
       name = object1;
       quantity = 10;
 },
 { 
       name = object2;
       quantity = 5;
 }

My ultimate goal is to be able to display the quantity next to the item in a table view. What is the best way to accomplish this?

4 Answers 4

3

Sounds like you want an NSCountedSet.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is great. However, I'm loading the data from a plist. So for example, object 1 would have a quantity of 10. I would then load a new plist with a count of 5. An NSCountedSet will obviously recognize these are two different objects since the quantity keys aren't the same. I can iterate through these, adding them to the set and stripping the quantity keys, but is there an easy way to get it back to an array for a table view while saving the counts?
@Brandon Schlenker: I think the point is that you wouldn't store the count in the dictionary. Instead, you would store the other attributes in the dictionary and use the counted set to answer the question "How many of this have I loaded?"
1

If you really want to iterate through your existing structure then you could do something like:

for (NSMutableDictionary *dict in ArrayName) {
    if([[dict valueForKey:@"name"] isEqual:someobject]) {
        int oldValue = [[dict valueForKey:@"quantity"] intValue];
        [dict setValue:[NSString stringWithFormat:@"%d",oldValue+1] forKey:@"quantity"];
    }
}

I assumed you stored the quantity as an NSString, you may have used something else like NSNumber.

I think a better idea is create your own class with two properties, a name and a quantity. Then hold one array of those objects and iterate through that.

Comments

0

Well since the keys for your dictionaries always look the same, I'd wonder if that's not actually a different dictionary:

{ object1: 10, object2: 5}

Assuming your object names are valid keys, I'd use those as keys, and then just increment the counters as needed when you get more objects to add.

2 Comments

The data is being read by a table view from an outside source. I'd really just like to be able to iterate through and combine the counts.
well, you can just use something like: [yourDictionary setObject:[NSNumber numberWithInt:[(NSInteger) [yourDictionary objectForKey:[tableDictionary objectForKey:@"name"]] intValue]+1] forKey:[tableDictionary objectForKey:@"name"]]
0

I do not have a clear idea of what you are exacly doing but,

you may solve this problem with just a dictionary (without an array) that stores you objects as its key and their count as its value.

so when you want to add an object, you first check to see if you alerady have that object -if you dont have it you add object1=1 -if you have it then you increment the value of that object1=2

1 Comment

The data is being read by a table view so I need to be able to call the "name' key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.