NSCountedSet initWithArray
- (id)initWithArray:(NSArray *)anArray
Parameters
anArray
An array of objects to add to the new set.
Return Value of [NSCountedSet initWithArray]
An initialized counted set object with the contents of anArray. The returned object might be different than the original receiver.
NSCountedSet initWithArray example.
- (void)testOccurrenceCounting
{
NSArray *numbers = @[@1, @3, @4, @6, @6, @3, @1, @3];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:numbers];
NSMutableArray *counters = [NSMutableArray arrayWithCapacity:[set count]];
[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
[counters addObject:[[Pair alloc] initWithKey:obj value:@([set countForObject:obj])]];
}];
[counters sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES]]];
NSLog(@"%@", counters);
}
{
NSArray *numbers = @[@1, @3, @4, @6, @6, @3, @1, @3];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:numbers];
NSMutableArray *counters = [NSMutableArray arrayWithCapacity:[set count]];
[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
[counters addObject:[[Pair alloc] initWithKey:obj value:@([set countForObject:obj])]];
}];
[counters sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES]]];
NSLog(@"%@", counters);
}
Example of [NSCountedSet initWithArray].
NSMutableArray *array = …;
NSCountedSet *countedSet = [[[NSCountedSet alloc] initWithArray:array]
autorelease];
// Array with distinct elements only, sorted by their repeat count
NSArray *distinctArray = [[countedSet allObjects]
sortedArrayUsingFunction:countedSort context:countedSet];
// Array with all the elements, where elements representing the same
// object are contiguous
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[array count]];
for (id object in distinctArray) {
for (NSUInteger i = 0; i < [countedSet countForObject:object]; i++) {
[sortedArray addObject:object];
}
}
NSCountedSet *countedSet = [[[NSCountedSet alloc] initWithArray:array]
autorelease];
// Array with distinct elements only, sorted by their repeat count
NSArray *distinctArray = [[countedSet allObjects]
sortedArrayUsingFunction:countedSort context:countedSet];
// Array with all the elements, where elements representing the same
// object are contiguous
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[array count]];
for (id object in distinctArray) {
for (NSUInteger i = 0; i < [countedSet countForObject:object]; i++) {
[sortedArray addObject:object];
}
}
NSCountedSet initWithArray example.
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];
}