0

I have two arrays: one for the total number of fruit consumed in the morning, and the other for the total fruit consumed for that day. What I'd like to do is display the changes/additions made since the morning and store it into a new array whatsNewSinceMorningArray. So first I need to remove any NSDictionary objects from both arrays that have the same fruit and totalNumberConsumed values (ignoring the comments), and then I was thinking to just loop through the wholeDayArray and subtract the totalNumberConsumed values to find the number consumed since the morning. I've read a couple of threads for removing duplicate NSDictionary objects inside an array, but I can't figure it out for my particular example. Would appreciate some guidance/point in the right direction.

NSArray *morningArray = [NSArray arrayWithObjects:
                   [NSDictionary dictionaryWithObjects:@[@"apples",@"1",@"sour"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],
                   [NSDictionary dictionaryWithObjects:@[@"oranges",@"3",@"sweet"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],
                   [NSDictionary dictionaryWithObjects:@[@"bananas",@"7",@"filling"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],nil];

NSArray *wholeDayArray = [NSArray arrayWithObjects:
                   [NSDictionary dictionaryWithObjects:@[@"apples",@"2",@"very sour"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],
                   [NSDictionary dictionaryWithObjects:@[@"oranges",@"3",@"slightly bitter"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],
                   [NSDictionary dictionaryWithObjects:@[@"bananas",@"8",@"filling"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],
                   [NSDictionary dictionaryWithObjects:@[@"pineapples",@"1",@"very sweet"]
                                               forKeys:@[@"fruit",@"totalNumberConsumed",@"comments"]],nil];

NSArray *whatsNewSinceMorningArray;
//Should end up with ["apples","1","very sour"],["bananas","1","filling"],["pineapples","1","very sweet"]
sooper
  • 5,991
  • 6
  • 40
  • 65
  • 2
    This sort of problem you need to figure out for yourself. It might be wise to "step back" a bit and think about it more abstractly -- draw pictures on the board, etc. Consider building a separate structure that abstracts the info you need to find the common entries, or even totally reconstruct your arrays vs just modifying them. – Hot Licks Sep 06 '13 at 12:15
  • Cheers @HotLicks, I'm thinking `NSMutableSet` may be a starting point, will give it a go. – sooper Sep 06 '13 at 12:23
  • Yeah, a set is probably as good a place to start as any. Just think it through, rather than trying to hack together examples. (Though certainly do build "toy examples" to test things you're not sure you understand.) – Hot Licks Sep 06 '13 at 12:26
  • 1
    It sounds like you need an object, why are you using a hash to store these properties in? – Oliver Atkinson Sep 06 '13 at 12:33
  • for starters you can write the above code using the new [notation](http://stackoverflow.com/a/9349981/766570).. rather than that God awful dictionaryWithObjcts:forKeys stuff.. doesn't make code very pleasant to look at – abbood Sep 06 '13 at 13:23

0 Answers0