views:

56

answers:

1

I'm able to put the contents of an NSSet into an NSMutableArray like this:

NSMutableArray *array = [set allObjects];

The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?

+3  A: 

Since -allObjects returns an array, you can create a mutable version with:

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

Or, alternatively, if you want to handle the object ownership:

NSMutableArray *array = [[set allObjects] mutableCopy];
dreamlax
@Eiko: No, `mutableCopy` must be balanced with a `release`. This excerpt from [`NSObject mutableCopy`](http://developer.apple.com/library/mac/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html) documentation: "The invoker of the method, however, is responsible for releasing the returned object."
dreamlax
Sorry, totally missed that - not sure what I was thinking.
Eiko