An NSSet is nothing more than a collection of objects, called members. The NSSet class contain a number of methods specifically designed to ease the creation and manipulation of its members. An NSSet object stores its members (which can be any kind of object) under a given name. Objects instantiated (created) from the NSSet class are immutable. This means once you’ve instantiated and initialize a set, you can’t add new members or delete existing members from it. Here are three things you should never, never forget about objects instantiated from the NSSet class.

  • Members of the set are not ordered.
  • A member can only appear once in a set, no matter how many times it is added.
  • Members of a set must be an Objective-C object, primitive C types, like int, float, etc. are not allowed.
To practice source code presented in this lesson, you should download and unzip this file.
project_icon_codepractice
Launch the project (CodePractice.xcodeproj) and enter code in the viewController.m file’s buttonTapped method, then click the “Click Me” button to execute the method’s code.

The setWithObjects:, member: and allObjects Methods

The setWithObjects: method instantiates an immutable set object.

The memberr method determines if  an object exist in the set, which you provide as it’s argument. The method returns that object if it exists in the set. By the way, the return data type of the member method is of type id. It is a general purpose data type that can be used to store a reference to any object, regardless of its type. Think of id as a wild card.

The allObjects method returns all members of a set as an unordered array. If the NSSet object has no members, the method will return an empty array.

Now, code below shows how to use all three methods.

Code Output
// Instantiate and initialize an NSSet object with six members
NSSet *colors = [NSSet setWithObjects:@"white",
@"orange",
@"red",
@"green",
@"white",
@"blue", nil];

// Display a member of the set
self.inputBoxOne.text = [colors member:@"orange"];

// Display all members of the set
self.outputBox.text = [[colors allObjects] componentsJoinedByString:@"\n"];
nsset_fig1

Despite being added twice, @”white” only appears in the set once.

The Sort Members of an NSSet

Now, since the allObjects method returns members of colors set as an unordered array, you must use the sortedArrayUsingSelector: method to put elements of the array in alphabetical order, as shown below.

Code Output
// Sort the set's members in alphabetical order
NSArray *sortedSet = [[colors allObjects] sortedArrayUsingSelector:@selector(compare:)];

// Convert the sortedSet array to a string object before displaying it in the UITextView control
self.outputBox.text = [sortedSet componentsJoinedByString:@"\n"];
nsset_fig2

Common Immutable Set Methods

The NSSet class contain several methods for manipulating instances (objects) of the class. You learned how to use three of them above. In this section I show you how to use two more methods of the NSSet class.

The anyObject Method

This method display any member of a set, as demonstrated below.

Code Output
self.outputBox.text = [colors anyObject];
  nsset_fig3

The containsObject Method

This method returns a Boolean value (YES/NO) indicating whether a given object exists in a set. You can then use the value in an if statement as shown below.

Code Output
BOOL colorExist = [colors containsObject:@"yellow"];

if(colorExist == YES)
  self.outputBox.text = @"That color does exist in the set.";
else
  self.outputBox.text = @"Sorry, that color does not exist in the set.";
nsset_fig4

The setWithArray: Method

This method turns an NSArray into an NSSet. Since duplicate members are not allowed in a set, you can use the setWithArray method to remove duplicate elements that exist in an NSArray or NSMutableArray object.

Code Output
NSArray *novels = @[@"The Passage",
@"Hush",
@"Lasher",
@"The Almost Moon",
@"The Passage",
@"Interview With The Vampire"];

NSSet *uniqueNovels = [NSSet setWithArray:novels];

self.outputBox.text = [[uniqueNovels allObjects] componentsJoinedByString:@"\n"];
nsset_fig5

Notice how the setWithArray: method on code line  above, removed the duplicate element, The Passage from the novels array.

No Responses

Leave a Reply