【问题标题】:Converting NSMutableSet to NSSet将 NSMutableSet 转换为 NSSet
【发布时间】:2017-01-26 19:24:02
【问题描述】:

有没有办法将 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];

【问题讨论】:

    标签: objective-c type-conversion nsset nsmutableset


    【解决方案1】:

    NSMutableSet 中的copy 返回一个 NSSet

    在您的示例中:

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

    【讨论】:

      【解决方案2】:
      -(NSSet *)ContainsNoShow:(NSMutableArray *)checkset
      {
      
        NSSet *newcheckset = [[NSSet alloc] init];
        newcheckset = [NSSet setWithArray:NoShows];
      
        NSMutableSet *checksetM     = [NSMutableSet setWithArray:checkset];
      
        [checksetM minusSet: newcheckset];
      
        return checksetM;
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-21
        • 2014-08-16
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        相关资源
        最近更新 更多