How to return an NSMutableArray from an NSSet
Asked Answered
P

3

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?

Photostat answered 30/9, 2010 at 0:34 Comment(0)
B
225

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];
Bahamas answered 30/9, 2010 at 0:41 Comment(1)
Note: there is a subtle difference in the two methods if set is nil.Worshipful
N
-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.

Nidanidaros answered 15/9, 2014 at 9:54 Comment(0)
B
-3

For an ordered set use:

NSArray *myArray = [[myOrderedSet array] mutableCopy];
Brownout answered 17/9, 2013 at 6:7 Comment(1)
There doesn't appear to be an NSSet - array method.Geld

© 2022 - 2024 — McMap. All rights reserved.