Converting NSMutableSet to NSSet

1.5k Views Asked by At

Is there a way to convert NSMutableSet to NSSet? I have tried several methods: moving to an NSArray and the setWithArray; instantiating an NSSet with the contents of the NSMutableSet. The program compiles but I get a run time error.

 NSMutableArray  *temp = [[NSMutableArray alloc] init];
 NSMutableSet    *num1 = [[NSMutableSet alloc] init];
 NSArray         *num2 = [[NSArray alloc] init];
 NSSet           *num3 = [[NSSet alloc] init];

 num1 = [checkset mutableCopy];  //checkset is of type NSSet
 num2 = [NSArray arrayWithArray:NoShows];
 num3 = [NSSet setWithArray:num2];

 [num1 minusSet:num3];
2

There are 2 best solutions below

0
Yugal Jindle On BEST ANSWER

copy of NSMutableSet returns a NSSet

In your sample:

NSSet *immutableSet = [checksetM copy]; // returns immutable copy
0
Kevin McFadden On
-(NSSet *)ContainsNoShow:(NSMutableArray *)checkset
{

  NSSet *newcheckset = [[NSSet alloc] init];
  newcheckset = [NSSet setWithArray:NoShows];

  NSMutableSet *checksetM     = [NSMutableSet setWithArray:checkset];

  [checksetM minusSet: newcheckset];

  return checksetM;

}