Initializing a Counted Set

- initWithArray:

Returns a counted set object initialized with the contents of a given array.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithArray:@[@"Eezy",@"Eezy",@"Tutorials",@"Website"]];
NSLog(@"%@",countedSet);
		

Output

2014-04-09 05:54:12.265 iOS-Tutorial[3532:a0b]  (Eezy [2], Tutorials [1], Website [1])		

- initWithSet:

Returns a counted set object initialized with the contents of a given set.

Example

NSSet *set = [NSSet setWithArray:@[@"Eezy",@"Eezy",@"Tutorials",@"Website"]];
NSCountedSet *countedSet = [[NSCountedSet alloc]initWithSet:set];
NSLog(@"%@",countedSet);
		

Output

2014-04-09 06:00:19.114 iOS-Tutorial[3558:a0b]  (Eezy [1], Tutorials [1], Website [1])
		

- initWithCapacity:

Returns a counted set object initialized with enough memory to hold a given number of objects.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithCapacity:5];
NSLog(@"%@",countedSet);
		

Output

2014-04-09 06:01:15.302 iOS-Tutorial[3579:a0b]  ()
		

Adding and Removing Entries

- addObject:

Adds a given object to the set.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithCapacity:5];
[countedSet addObject:@"Eezy"];
[countedSet addObject:@"Eezy"];
NSLog(@"%@",countedSet);
		

Output

2014-04-09 06:03:08.805 iOS-Tutorial[3594:a0b]  (Eezy [2])
		

- removeObject:

Removes a given object from the set.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithCapacity:5];
[countedSet addObject:@"Eezy"];
[countedSet removeObject:@"Eezy"];
NSLog(@"%@",countedSet);
		

Output

2014-04-09 06:04:14.707 iOS-Tutorial[3608:a0b]  ()
		

Examining a Counted Set

- countForObject:

Returns the count associated with a given object in the set.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithCapacity:5];
[countedSet addObject:@"Eezy"];
[countedSet addObject:@"Eezy"];
NSLog(@"%d",[countedSet countForObject:@"Eezy"]);
		

Output

2014-04-09 06:06:36.694 iOS-Tutorial[3624:a0b] 2
		

- objectEnumerator

Returns an enumerator object that lets you access each object in the set once, independent of its count.

Example

NSCountedSet *countedSet = [[NSCountedSet alloc]initWithObjects:@[@"Eezy",@"Eezy",@"Tutorials",@"Website"], nil];
NSEnumerator *enumerator = [countedSet objectEnumerator];
for (NSString *aValue in enumerator) {
    NSLog(@"%@",aValue);
}		

Output

2014-04-09 06:10:35.024 iOS-Tutorial[3655:a0b] (
    Eezy,
    Eezy,
    Tutorials,
    Website
)		

Advertisements