i have an array of dictionary objects with repeated objects, i need to make another array with no repeated objects with another key that represents the number of times that object was repeated.
Example:
object A (key1, key2)
object B (key1, key2)
object C (key1, key2)
object B (key1, key2)
object B (key1, key2)
Result,with new dictionary array:
object A (key1, key2, count(1))
object B (key1, key2, count(3))
object C (key1, key2, count(1))
Im having a hard time since im new to NSDictionary objects, the code so far:
for(int j = 1; j < [objects count]; j++)
{
for(int k = j+1;k < [objects count]-j;k++)
{
NSMutableDictionary *item1 = [objects objectAtIndex:j];
NSMutableDictionary *item2 = [objects objectAtIndex:k];
NSString *str1 = [item1 valueForKey:@"setArrivalAirportOrCode"];
NSString *str2 = [item2 valueForKey:@"setArrivalAirportOrCode"];
NSLog(@"%@==%@ ???",str1,str2);
if([str1 isEqualToString:str2])
NSLog(@"%@==%@",str1,str2);
}
}
Im trying to count the objects and copy them to a new array, i am not sure if i am going in the right direction, sorry for my poor English and please don’t down-vote, just ask me anything i am always online.
Thanks in Advance!
Create a NSCountedSet and it will take care of the counting for you.
In this example, I used NSString objects but it can be done with any type of object:
In order to get the count, you use:
In order to get the results in a separate array (note that you lose the counts, so use the counted set for that), use: