Search code examples
objective-ctype-conversionnssetnsmutableset

Converting NSMutableSet to NSSet


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

Solution

  • copy of NSMutableSet returns a NSSet

    In your sample:

    NSSet *immutableSet = [checksetM copy]; // returns immutable copy