将NSMutableSet转换为NSSet

时间:2017-01-26 19:24:02

标签: objective-c type-conversion nsset nsmutableset

有没有办法将NSMutableSet转换为NSSet?我尝试了几种方法:转移到NSArray和setWithArray;使用NSMutableSet的内容实例化NSSet。该程序编译但我得到运行时错误。

 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 个答案:

答案 0 :(得分:3)

copy

NSMutableSet返回NSSet

在您的样本中:

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

答案 1 :(得分:0)

-(NSSet *)ContainsNoShow:(NSMutableArray *)checkset
{

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

  NSMutableSet *checksetM     = [NSMutableSet setWithArray:checkset];

  [checksetM minusSet: newcheckset];

  return checksetM;

}