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).

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);

Subscribe to our youtube channel to get new updates..!

//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