Open In App

NSSet and NSMutableSet in Objective-C

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

NSSet and NSMutableSet are two classes in Objective-C that are used for representing collections of objects. Both classes provide an ordered set of unique objects, with the difference being that NSSet is immutable, while NSMutableSet is mutable, which means its contents can be changed dynamically. In this article, we will be exploring the NSSet and NSMutableSet classes in detail.

NSSet

NSSet is a class that represents an unordered collection of unique objects. The objects stored in a set must be of the same type and cannot be duplicated. NSSet provides several methods for adding, removing, and searching for objects, as well as for determining the number of objects in the set and checking for the presence of specific objects.

Syntax:

In Objective-C, creating a set is done using the NSSet classes. To create an instance of NSSet, the following syntax can be used:

NSSet *set = [NSSet setWithObjects:obj1, obj2, obj3, nil];

NSMutableSet

NSMutableSet is a subclass of NSSet and provides the same functionality as NSSet, with the addition of the ability to modify its contents. NSMutableSet provides methods for adding and removing objects, as well as for removing all objects from the set.

Syntax:

In Objective-C, creating a set is done using the NSMutableSet classes. To create an instance of NSMutableSet, the following syntax can be used:

NSMutableSet *mutableSet = [NSMutableSet setWithObjects:obj1, obj2, obj3, nil];

Below is a table showing some of the commonly used methods of NSSet and NSMutableSet with their descriptions:

Method NameDescription
initWithObjectsInitializes an NSSet object with the specified objects.
setWithObjectsReturns a new NSSet object with the specified objects.
countReturns the number of objects in the set
containsObjectChecks whether the set contains a specific object.
allObjectsReturns an array holding all of the set's items.
addObjectAdds an object to the set
removeObjectRemoves an object from the set
removeAllObjectsRemoves all objects from the set

Example 1: Introduction to NSMutableSet.

ObjectiveC
#import <Foundation/Foundation.h>

int main() {
   // Create an empty NSMutableSet
   NSMutableSet *set = [NSMutableSet set];

   // Add objects to the set
   [set addObject:@"Apple"];
   [set addObject:@"Banana"];
   [set addObject:@"Mango"];

   // Print the contents of the set
   NSLog(@"Set: %@", set);

   return 0;
}

Output:

output
 

The above code creates an empty NSMutableSet and adds three objects to it. The contents of the set are then printed using NSLog.

Example 2: Creating an NSSet and NSMutableSet

ObjectiveC
#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // Creating an NSSet
   NSSet *set = [NSSet setWithObjects:@"Apple", 
                 @"Banana", @"Cherry", nil];

   // Printing the set
   NSLog(@"NSSet: %@", set);

   // Creating an NSMutableSet
   NSMutableSet *mutableSet = [NSMutableSet setWithObjects:@"Apple", 
                               @"Banana", @"Cherry", nil];

   // Adding an object to the NSMutableSet
   [mutableSet addObject:@"Date"];

   // Printing the mutable set
   NSLog(@"NSMutableSet: %@", mutableSet);

   [pool drain];
   return 0;
}

Output:

output
 

In the first part of the example, an NSSet is created with three objects, and its contents are printed using NSLog. In the second part, an NSMutableSet is created with the same three objects as the NSSet, and an additional object "Date" is added to it. The contents of the NSMutableSet are then printed using NSLog.


Article Tags :

Similar Reads