Reputation: 55
If there is an NSArray with nth count, I need to fetch first 3 element and then next 3 element so on.
example:- [A,B,C,D,E,F,G]
result1:- [A,B,C]
result2:- [D,E,F]
result3:- [G]
I have array of nth element for which I wanted to fetch each 3 element from beginning of the array till nth element.
Tried code,
myArr=[[NSMutableArray alloc]initWithObjects:@"k",@"L",@"M",@"N",@"O",@"P",@"Z",nil];
NSArray *getData=[myArr subarrayWithRange:NSMakeRange(i, MIN(3, myArr.count))];
i=i+3;
Upvotes: 0
Views: 88
Reputation: 7552
Try the following.
NSArray *a = @[ @1, @2, @3, @4, @5, @6, @7 ];
int start = 0;
int slice_size = 3;
while (start < a.count) {
NSRange range = NSMakeRange(start, slice_size);
if (range.location + range.length >= a.count) {
range.length = a.count - range.location;
}
NSArray *slice = [a subarrayWithRange:range];
NSLog(@"slice: %@", slice);
start += slice_size;
}
Upvotes: 1