106

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?

Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38
node ninja
  • 31,796
  • 59
  • 166
  • 254

3 Answers3

224

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
  • 93,976
  • 29
  • 161
  • 209
-1

I resolved crashing by using NSMutableArray's method 'addObjectsFromArray' to assign all NSSet objects to NSMutableArray like:

[mutableArray addObjectsFromArray:[cg_Schedule.schedule_Days allObjects]];

Hope this will helps you.

Irfan
  • 4,301
  • 6
  • 29
  • 46
-3

For an ordered set use:

NSArray *myArray = [[myOrderedSet array] mutableCopy];
Dustin
  • 213
  • 2
  • 5