Home  >  Blog  >   iPhone  > 

Exploring the Foundation Framework in iPhone - NS Set Class

Rating: 4



The NSSet, NSMutableSet, and NSCountedSet classes declare the programmatic interface to an unordered collection of objects.

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

NSSet declares the programmatic interface for static sets of distinct objects. You establish a static set’s entries when it’s created, and thereafter the entries can’t be modified. NSMutableSet, on the other hand, declares a programmatic interface for dynamic sets of distinct objects. A dynamic—or mutable—set allows the addition and deletion of entries at any time, automatically allocating memory as needed. ( Explore the types of Foundation Framework Classes).

MindMajix Youtube Channel

You can use sets as an alternative to arrays, when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.

   NSSet *a set = [[NSSet alloc]init with objects:@ “first”, @ “second”, @third”, nil];
Ns log (@ “a set is %@”,a set);

//Using Class method:

   NSSet *b set   =[NSSet SetWithObjects :@  “hi” @ “hello, nil];
NSLog (@  “the set value is % @”, bset);

//Initializing with other set:

NSSet *CSet  =  [NSSet SetWithSet :aSet];
NSLog  (@   “the set value is %@”, c set);

// count:

Int  Count   =  [a set Count];

//adding   sets to an array:

NSArray  * SetArray =
[ NSArray array with objects: aset, bset, cset, nil];
NSLog (@  “the array value is %@”, SetArray);

//Getting an set element:

NSString  *str  = [aSet   anyobject];
NSLog (@ “the set elements is %@”, str);

Converting to array:

   NSArray   * SetArray     =  [aSet allObjects];
NSLog (@”the SetArray is %@”, SetArray);

// adding or doing set operations:

NSMutableSet *a mutSet = [NSMutable set with set: a set];

// adding:

[ a mutSet  add object: @ “good”];

// removing:

[ a mutSet remove object : @ “good”];

// removing all objects:

[ a mutSet remove all objects];
Ns log (@ “the removing a is %@, a mutSet);

// add object from array:

[ a mutSet addObjectFromArray: aArray];
NSLog (@ “ the object is % @”, a mutSet);

// checking for object:

      If ([a mutSet contains object : @ “good”])
     // good is there in the mutSet

// union:

    [ a mutSet  unionSet :a set];
     NSLog (“@”the union value is a mutSet);

// Intersection

  [ a mutSet   intersectionSet :a set ];
NSLog (@ “the insert value is %@”, a mutSet);


  [a mutSet intersectSet : bSet];
Ns log (@ “ the minus value is %@”, a mutSet);


Frequently Asked iPhone Interview Questions & Answers


Join our newsletter

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
IOS Development TrainingMar 25 to Apr 09
IOS Development TrainingMar 28 to Apr 12
IOS Development TrainingApr 01 to Apr 16
IOS Development TrainingApr 04 to Apr 19
Last updated: 22 March 2023
About Author
Remy Sharp
Ravindra Savaram

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

Recommended Courses

1 /15