如何从 NSSet 返回 NSMutableArray

我可以把一个 NSSet的内容放到一个像这样的 NSMutableArray里:

NSMutableArray *array = [set allObjects];

但是编译器会抱怨,因为[ set allObjects ]返回的是 NSArray而不是 NSMutableArray。这个问题应该如何解决?

64810 次浏览

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];

For an ordered set use:

NSArray *myArray = [[myOrderedSet array] mutableCopy];

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.