Objective C Tutorial - Objective C Set






Create a Set

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSSet *set = [NSSet setWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSLog(@"set: %@",set);
    return 0;
}

The code above generates the following result.





Accessing all Items in a Set

#import <Foundation/Foundation.h>

int main ()
{
  NSSet *mySet;
    mySet = [NSSet setWithObjects: @"A", @"M", @"S", @"W", nil];
    NSLog (@"Set members = %i", [mySet count]);
    
    id testObject;
    for (testObject in mySet)
  {
    NSLog (@"Member = %@", testObject);
  }
    
    return 0;
}

The code above generates the following result.





Adding and Removing Data in a Set

#import <Foundation/Foundation.h>

int main ()
{
  NSMutableSet *mySet;
    mySet = [NSMutableSet setWithObjects: @"J", @"M", @"S", @"O", nil];
    NSLog (@"Set members = %i", [mySet count]);
    
    [mySet addObject: @"Bo"];
    NSLog (@"Set members = %i", [mySet count]);
    
    NSString *object1 = @"Hello";
    NSString *object2 = @"world!";
    NSNumber *object3 = [NSNumber numberWithInt:45];
    NSArray *myArray = [NSArray arrayWithObjects: object1,  object2, object3, nil];
    
    [mySet addObjectsFromArray: myArray];
    NSLog (@"Set members = %i", [mySet count]);
    
    [mySet removeObject:@"Joe"];
    NSLog (@"Set members = %i", [mySet count]);
    
    [mySet removeAllObjects];
    NSLog (@"Set members = %i", [mySet count]);  
    
    return 0;
}

The code above generates the following result.

Checking if Data is in a Set

#import <Foundation/Foundation.h>
 
int main ()
{
  NSSet *mySet;
    mySet = [NSSet setWithObjects: @"Joe", @"Mary", @"Sue", @"Olly", nil];
    NSLog (@"Set members = %i", [mySet count]);
    if ([mySet containsObject:@"Joe"])
  {
    NSLog (@"Found member in set");
  }
  else
  {
    NSLog (@"No member found in set");
  }
 
   return 0;
}

The code above generates the following result.

Identifying a Subset

#import <Foundation/Foundation.h>
 
int main ()
{
  NSSet *mySet;
    mySet = [NSSet setWithObjects: @"Joe", @"Mary", @"Sue", @"Olly", nil];
    NSLog (@"Set members = %i", [mySet count]);
    
    NSSet *newSet;
    newSet = [NSSet setWithObjects: @"Bill", @"Mary", nil];
    
    if ([mySet intersectsSet: newSet])
    {
        NSLog (@"Found a match");
    }
    else
    {
        NSLog (@"No match");
    }
    
    NSSet *thirdSet;
    thirdSet = [NSSet setWithObjects: @"Joe", @"Mary", nil];
    
    if ([mySet isSubsetOfSet: newSet])
    {
        NSLog (@"Subset found");
    }
    else
    {
        NSLog (@"No subset");
    }
  
    if ([thirdSet isSubsetOfSet: mySet])
    {
        NSLog (@"Subset");
    }
    else
    {
        NSLog (@"No subset");
    }
 
   return 0;
}

Iterating Sets


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSSet *set = [NSSet setWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        for (NSString *s in [set allObjects]) {
            NSLog(@"value: %@", s);
        }
        
        [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
            NSLog(@"obj = %@", obj);
        }];
        
        [set makeObjectsPerformSelector:@selector(description)];
        
    return 0;
}

The code above generates the following result.

Comparing Sets

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSSet *set1 = [NSSet setWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
        
        NSSet *set2 = [NSSet setWithObjects:@"D", @"E", @"F", @"G", @"H", nil];
        
        NSLog(@"set1 contains:%@", set1);
        
        NSLog(@"set2 contains:%@", set2);
        
        BOOL setsIntersect = [set1 intersectsSet:set2];
        
        BOOL set2IsSubset = [set2 isSubsetOfSet:set1];
        
        BOOL set1IsEqualToSet2 = [set1 isEqualToSet:set2];
        
        BOOL set1ContainsD = [set1 containsObject:@"D"];
        
        NSLog(@"setsIntersect = %i, set2IsSubset = %i, set1IsEqualToSet2 = %i, set1ContainsD = %i", 
              setsIntersect, set2IsSubset, set1IsEqualToSet2, set1ContainsD);
    return 0;
}

The code above generates the following result.

Count a Set

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSSet *set = [NSSet setWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
        
        NSUInteger count = set.count;
        
        NSLog(@"The set contains %lu items", count);
        
    return 0;
}

The code above generates the following result.