kiran kiran - 12 days ago 9
iOS Question

Added NSMutableArray elements into another NSMutableArray in jumble way

I have

NSMutableArray
of 100 elements in it.

I want to load "20 - 50", "10 - 20", "60 - 100" and 50 - 60. elements into separate NSMutableArray and its Index value into another
NSMutableArray
.

is there away I can load
20 - 50
elements 30 count items after the
10 - 20
elements 10 count items then
60 - 100
items i.e 40 count finally
50 - 60
items 10 count items added into one new
NSmutableArray
and there index storied value into another
NSmutableArray
.

Is there a way i can add the items

NSMutableArray *valueArray; // Value array count is 100.

NSMutableArray *indexValue;
NSMutableArray *addValueRangeArray;
for (int i = 0; i< [ valueArray count ]; i ++) {

if (i == 20 && i <= 50 )
{
[addValueRangeArray addObject:[valueArray objectAtIndex:i]];
[indexValue addObject:i];
}
}


Here its not working since for count loop starts 0 to 99
and items added starts from 0

I want to added item with range base where i do get objects added and also the index value too.

Your input are highly appreciated
Let me know if not understand the question.
Since its tricky one.

Answer

You can use subarrayWithRange to get specific range from array.

if (valueArray.count >= 100) {
     NSMutableArray *rangeArray = [[NSMutableArray alloc] init];
     [rangeArray addObjectsFromArray:[valueArray subarrayWithRange:NSMakeRange(20, 30)]];
     //and so on.
}

Note: With NSMakeRange passed first parameter as range's starting point and second one is length so pass the number of objects for that range, so for first range 20-50 it is NSMakeRange(20, 30). It may cause you crash if your valueArray doesn't have objects with that range.