13
array = [NSMutableArray arrayWithArray:[set allObjects]];

This worked with an NSSet, but how do I get it to work with an NSMutableOrderedSet?

Adrian
  • 16,233
  • 18
  • 112
  • 180
Landon
  • 787
  • 1
  • 9
  • 19

1 Answers1

34

there is a method on the NSOrderedSet class to get an array [orderedSet array]. you can then do [array mutableCopy] to get a mutable array.

or use [NSMutableArray arrayWithArray:[set array]]; if you prefer.

do be aware of this, however:

This return a proxy object for the receiving ordered set, which acts like an immutable array. While you cannot mutate the ordered set through this proxy, mutations to the original ordered set will be reflected in the proxy and it will appear to change spontaneously, since a copy of the ordered set is not being made.

basically you must copy the array if you don't want this behavior.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • I had tried [NSMutableArray arrayWithArray:[set array]]; before and it was giving me an -[__NSSetM array]: unrecognized selector error message. Not sure why – Landon Oct 22 '13 at 04:45
  • 1
    that is because __NSSetM is an NSMutableSet, not an NSMutableOrderedSet. you may be confused thinking that NSMutableOrderedSet descends from NSSet, but it does not.Anyway this is the correct answer for getting an NSMutableArray from an NSMutableOrderedSet as asked. So which do you need an answer for, NSMutableSet or NSMutableOrderedSet? – Brad Allred Oct 22 '13 at 04:50
  • Thanks. Just realized I was allocating it as an NSMutableSet and not an NSMutableOrderedSet. – Landon Oct 22 '13 at 04:54