Search code examples
objective-cswiftnsset

IOS - Convert from NSSet to NSMutableSet


Given an NSSet what is the best way to get an NSMutableSet with those same objects?

To be clear if you have an instance of NSSet what would be the most performant and simple way to get an NSMutable set with those same objects?


Solution

  • There are 2 ways to achieve it.

    • Use mutableCopy

      Objective-C

      NSMutableSet* mutableSet2 = [YOUR_SET mutableCopy];
      

      Swift

      let mutableSet = YOUR_SET.mutableCopy()
      
    • Use setWithSet:

      Objective-C

      NSMutableSet* mutableSet = [NSMutableSet setWithSet:YOUR_SET];
      

      Swift

      let mutableSet = NSMutableSet.init(set: YOUR_SET)