Split an NSMutableArray into other NSMutableArrays

喜欢而已 提交于 2019-12-10 21:23:34

问题


I have an NSMutableArray with 50 entries - is there any easy way to split this into 5 NSMutableArrays each with 10 entries each.


回答1:


I wrote a short function to do this a while ago. The gist of it is to loop an array filling an arbitrary array at the loop index modulo the number of subarrays you want until you run out of items.




回答2:


Yes To divide the NSMutableArray you can use the NSArray method

- (NSArray *)subarrayWithRange:(NSRange)range

NSRange is a struct that is a start location and number of items. So you would want 0 and 10, 10 and 10, 20 and 10, etc.

Use the following function to make your arrays:

NSRange NSMakeRange (NSUInteger loc, NSUInteger len );

Hope that helps.



来源:https://stackoverflow.com/questions/17117339/split-an-nsmutablearray-into-other-nsmutablearrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!