I’m trying to create an NSDictionary of unique keys with each value being the count of the unique keys.
i.e.
Given the array [apple, apple, banana, apple, pear, banana] I’d get the dictionary…
{
apple : 3,
banana : 2,
pear : 1
}
I’m sure I’ve seen code before where you can do something like…
NSMutableArray *uniqueKeys = [NSMutableArray array];
for (NSString *string in myArray) {
uniqueKeys[string]++;
}
But I can’t remember what the exact syntax is.
I know I could do it long hand by extracting the NSNumber and changing it and putting it back but I wanted to use this method if possible.
Seems like you may be able to use NSCountedSet.
If “apple” and “apple” are equal, you’ll be able to know both how many “fruits” are in your counted-set as well as how many “apple”s are in the set.
Apple’s Documentation:
Update
Here is an example:
Which will output: