Create empty NSSet

NSSet *set = [NSSet set];

Create NSSet with one object

NSSet *set = [NSSet setWithObject:@"One"];

Create NSSet with several objects

NSSet *set = [NSSet setWithObjects:@"One", @"Two", @"Three", nil];

Create NSSet with elements of array

NSSet *set = [NSSet setWithArray:array];

Create set with another set

NSSet *set = [NSSet setWithSet:anotherSet];

Add object to immutable set

NSSet *newSet = [set setByAddingObject:@"Four"];

Add objects from another set to immutable set

NSSet *newSet = [set setByAddingObjectsFromSet:anotherSet];

Add objects from array to immutable set

NSSet *newSet = [set setByAddingObjectsFromArray:array];

Number of objects in the set

NSUInteger total = [set count];

Get all elements of the set as array

NSArray *array = [set allObjects];

Get any object from the set

id obj = [set anyObject];

Check if the object is a member of the set

BOOL b = [set containsObject:obj];

Get element of the set that is equal to given object

// [m isEqual:obj] evaluates to YES
id m = [set member:obj];

Filter objects in the set using predicate

// people contains objects with NSString property "city"
NSPredicate *cityPredicate = [NSPredicate predicateWithFormat:@"city = 'Vancouver'"];
NSSet *vancouverites = [people filteredSetUsingPredicate:cityPredicate];

Perform selector on every object in the set

[set makeObjectsPerformSelector:@selector(ping)];

Perform selector with argument on every object in the set

[set makeObjectsPerformSelector:@selector(ping:) withObject:self];

Enumerate objects in the set using block

[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
    // Do something useful

Find all object in the set using test block

NSSet *set = [NSSet setWithObjects:@"1", @"22", @"33", nil];

// Result is ["22", "33"]
NSSet* passedTest = [set objectsPassingTest:^BOOL(id obj, BOOL *stop) {
    if ([obj length] > 1)
        return YES;
        return NO;

Check if the set is subset of another set

NSSet *set1 = [NSSet setWithObjects:@"1", @"2", @"3", nil];
NSSet *set2 = [NSSet setWithObject:@"3"];
// Result is YES
BOOL subset = [set2 isSubsetOfSet:set1];

Check if sets intercect

NSSet *set1 = [NSSet setWithObjects:@"1", @"2", @"3", nil];
NSSet *set2 = [NSSet setWithObjects:@"3", @"4", @"5", nil];
// Result is YES
BOOL intersect = [set2 isSubsetOfSet:set1];

Check if sets are equal

NSSet *set1 = [NSSet setWithObjects:@"1", @"2", @"3", nil];
NSSet *set2 = [NSSet setWithObjects:@"3", @"4", @"5", nil];
// Result is NO
BOOL intersect = [set2 isEqualToSet:set1];

Map set elements to another set

NSSet *persons;
// init persons with KVC-compliant objects
NSSet *names = [persons valueForKey:@"name"];

Set property of all elements in the set

NSSet *persons;
// init persons with KVC-compliant objects
[persons setValue:@"Vancouver" forKey:@"location"];

Sort content of the set using sort descriptors

NSArray *descriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"relevance" ascending:YES]];
// objects in set should be KVC-compliant
NSArray *sortedElements = [set sortedArrayUsingDescriptors:descriptors];

Get property list representation of the set

NSString *desc = [set description];

Create mutable copy of the set

NSMutableSet *mutable = [set mutableCopy];