开发者

How to return an NSMutableArray from an NSSet

开发者 https://www.devze.com 2023-01-18 16:40 出处:网络
I\'m able to put the contents of an NSSet into an NSMutableArray like this: NSMutableArray *arra开发者_JAVA技巧y = [set allObjects];

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

NSMutableArray *arra开发者_JAVA技巧y = [set allObjects];

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


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


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.


For an ordered set use:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号