sol*_*eil 0 nsdictionary nsarray nssortdescriptor ios nscountedset
我迷失在排序描述符,词典和计数集的海洋中.希望有人可以帮我找到一个更简单的方法.
从这样的排序数组开始(NSNumbers):
['7','7','7','5','5','5','5','5','5','3','2','2','2','2']
Run Code Online (Sandbox Code Playgroud)
我想最终得到一个排序的字典数组,如下所示:
{'5','6'} //the number 5 appeared 6 times
{'2','4'} //the number 2 appeared 4 times
{'7','3'} //the number 7 appeared 3 times
{'3','1'} //the number 3 appeared 1 time
Run Code Online (Sandbox Code Playgroud)
这是一个片段,基于你做你想要的 NSCountedSet
NSArray *numbers = @[@7,@7,@7,@5,@5,@5,@5,@5,@5,@3,@2,@2,@2,@2];
NSCountedSet *countedSet = [NSCountedSet setWithArray:numbers];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (NSNumber *num in countedSet) {
NSUInteger c = [countedSet countForObject:num];
[result setObject:@(c) forKey:num];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |