NSMutableOrderedSet to NSMutableArray

Landon picture Landon · Oct 22, 2013 · Viewed 11.6k times · Source
array = [NSMutableArray arrayWithArray:[set allObjects]];

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

Answer

Brad Allred picture Brad Allred · Oct 22, 2013

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.