NSSet and NSMutableSet in Objective-C
Last Updated :
28 Apr, 2025
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 Name | Description |
---|
initWithObjects | Initializes an NSSet object with the specified objects. |
setWithObjects | Returns a new NSSet object with the specified objects. |
count | Returns the number of objects in the set |
containsObject | Checks whether the set contains a specific object. |
allObjects | Returns an array holding all of the set's items. |
addObject | Adds an object to the set |
removeObject | Removes an object from the set |
removeAllObjects | Removes 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:
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:
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.
Similar Reads
NSArray and NSMutableArray in Objective-C An array is a data structure that allows to store data in contiguous memory. They can be used to store primitive data types such as int, float, double, char, etc of any particular type or can store derived data types such as structures, pointers, etc. Its indexing starts with 0 i.e. the first elemen
5 min read
NSDictionary and NSMutableDictionary in Objective-C Dictionary in Objective-C stores objects in a key-value pair. The data items stored in a dictionary can be accessed using its key. It is dynamic in nature means the size of the dictionary grows according to the need. In Dictionary, the key cannot be null, but the value can be NULL. It can be mutable
5 min read
Numbers in Objective-C Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Memory Management in Objective-C Memory management is a core concept irrespective of any programming language. The process of allocation of memory of objects when needed and deallocation when they are no longer in use is called Memory Management. Memory is a limited resource and so has to be dealt with carefully. If unrequired memo
7 min read
Inheritance in Objective-C In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
Dates and Times in Objective-C In Objective-C, dates and times are represented using the NSDate and NSDateFormatter classes. NSDate class represents a single point in time. It stores an absolute point in time, independent of any particular calendar or time zone. You can use NSDate to represent a date and time, or just a date or t
4 min read