NSCountedSet countForObject
- (NSUInteger)countForObject:(id)anObject
Parameters
anObject
The object for which to return the count.
Return Value of [NSCountedSet countForObject]
The count associated with anObject in the set, which can be thought of as the number of occurrences of anObject present in the set.
NSCountedSet countForObject example.
The sample below ...
NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];
for (id item in set)
{
NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
}
NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names];
for (id item in set)
{
NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]);
}
Example of [NSCountedSet countForObject].
countedList = [[NSCountedSet alloc] initWithArray:listOfItems];//listOfItems is an array of country name strings with duplicates
listOfItems2 = [NSMutableArray array]; // array without duplicates that's the data source for the UITableView
for (NSString *aCountry in countedList) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:aCountry forKey:@"name"];
[dict setValue:[NSString stringWithFormat:@"%lu",[countedList countForObject:aCountry]] forKey:@"count"];
[listOfItems2 addObject:dict];
}
listOfItems2 = [NSMutableArray array]; // array without duplicates that's the data source for the UITableView
for (NSString *aCountry in countedList) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:aCountry forKey:@"name"];
[dict setValue:[NSString stringWithFormat:@"%lu",[countedList countForObject:aCountry]] forKey:@"count"];
[listOfItems2 addObject:dict];
}
NSCountedSet countForObject example.
NSArray *arr = @[@1, @3, @4, @6, @6, @3, @1, @3];
NSCountedSet *totalSet = [NSCountedSet setWithArray:arr];
NSMutableArray *dictArray = [NSMutableArray array];
for (NSNumber *num in totalSet) {
NSDictionary *dict = @{@"number":num, @"count":@([totalSet countForObject:num])};
[dictArray addObject:dict];
}
NSArray *final = [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES ]]];
NSLog(@"%@",final);
NSCountedSet *totalSet = [NSCountedSet setWithArray:arr];
NSMutableArray *dictArray = [NSMutableArray array];
for (NSNumber *num in totalSet) {
NSDictionary *dict = @{@"number":num, @"count":@([totalSet countForObject:num])};
[dictArray addObject:dict];
}
NSArray *final = [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES ]]];
NSLog(@"%@",final);