How to slice NSMutableArray

user317033 picture user317033 · Mar 13, 2011 · Viewed 15.2k times · Source

I have a NSMutableArray and need objects [0:5] only. Is there a simple way to slice? Can I drop all objects after index? Can I copy a sub-array to another NSMutableArray?

Answer

James Bedford picture James Bedford · Mar 13, 2011

Use the instance method - (NSArray *)subarrayWithRange:(NSRange)range.

For example:

NSArray* slicedArray = [wholeArray subarrayWithRange:NSMakeRange(0, 5)];