Creating a Mutable Set

+ setWithCapacity:

Creates and returns a mutable set with a given initial capacity.

Example

	NSMutableSet *set = [NSMutableSet setWithCapacity:2];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:00:05.146 iOS-Tutorial[3560:a0b] {(
)}
		

- initWithCapacity:

Returns an initialized mutable set with a given initial capacity.

Example

	NSMutableSet *set = [[NSMutableSet alloc ]initWithCapacity:2];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:00:05.146 iOS-Tutorial[3560:a0b] {(
)}
		

- init

Initializes a newly allocated set.

Example

	NSMutableSet *set = [[NSMutableSet alloc ]init];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:00:05.146 iOS-Tutorial[3560:a0b] {(
)}
		

Adding and Removing Entries

- addObject:

Adds a given object to the set, if it is not already a member.

Example

	NSMutableSet *set = [NSMutableSet set];
    [set addObject:@"Eezy"];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:02:14.729 iOS-Tutorial[3575:a0b] {(
    Eezy
)}
	

- filterUsingPredicate:

Evaluates a given predicate against the set’s content and removes from the set those objects for which the predicate returns false.

Example

	NSMutableSet *set = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'E'"];
    NSSet *resultSet = [set filteredSetUsingPredicate:predicate];
    NSLog(@"%@",resultSet);
		

Output

2014-03-22 21:07:48.090 iOS-Tutorial[3616:a0b] {(
    Eezy
)}		

- removeObject:

Removes a given object from the set.

Example

	NSMutableSet *set = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    [set removeObject:@"Eezy"];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:04:13.380 iOS-Tutorial[3588:a0b] {(
    Tutorials
)}
		

- removeAllObjects

Adds to the set each object contained in a given array that is not already a member.

Example

	NSMutableSet *set = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    [set removeAllObjects];
    NSLog(@"%@",set);
		

Output

2014-03-22 21:04:45.058 iOS-Tutorial[3600:a0b] {(
)}
		

Combining and Recombining Sets

- unionSet:

Adds each object in another given set to the receiving set, if not present.

Example

	NSMutableSet *set1 = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    NSMutableSet *set2 = [NSMutableSet setWithObjects:@"Website",@"Tutorials", nil];
    [set1 unionSet:set2];
    NSLog(@"%@",set1);
		

Output

2014-03-22 21:11:22.628 iOS-Tutorial[3632:a0b] {(
    Eezy,
    Tutorials,
    Website
)}
		

- minusSet:

Removes each object in another given set from the receiving set, if present.

Example

	NSMutableSet *set1 = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    NSMutableSet *set2 = [NSMutableSet setWithObjects:@"Website",@"Tutorials", nil];
    [set1 minusSet:set2];
    NSLog(@"%@",set1);
		

Output

2014-03-22 21:12:05.148 iOS-Tutorial[3645:a0b] {(
    Eezy
)}
		

- intersectSet:

Removes from the receiving set each object that isn’t a member of another given set.

Example

	NSMutableSet *set1 = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    NSMutableSet *set2 = [NSMutableSet setWithObjects:@"Website",@"Tutorials", nil];
    [set1 intersectSet:set2];
    NSLog(@"%@",set1);
		

Output

2014-03-22 21:12:51.851 iOS-Tutorial[3656:a0b] {(
    Tutorials
)}
		

- setSet:

Empties the receiving set, then adds each object contained in another given set.

Example

	NSMutableSet *set1 = [NSMutableSet setWithObjects:@"Eezy",@"Tutorials", nil];
    NSMutableSet *set2 = [NSMutableSet setWithObjects:@"Website",@"Tutorials", nil];
    [set1 setSet:set2];
    NSLog(@"%@",set1);
		

Output

2014-03-22 21:13:37.954 iOS-Tutorial[3668:a0b] {(
    Tutorials,
    Website
)}
		

Advertisements