简体   繁体   中英

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

copy of NSMutableSet returns a NSSet

In your sample:

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

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

  NSMutableSet *checksetM     = [NSMutableSet setWithArray:checkset];

  [checksetM minusSet: newcheckset];

  return checksetM;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM