Nilam Goswami Nilam Goswami - 2 months ago 8
Objective-C Question

copy some index of one array to another array

I have this array,

SnegatvSph = [NSArray arrayWithObjects:@"-0.25",@"-0.50",@"-0.75",@"-1.00",@"-1.25",@"-1.50",@"-1.75",@"-2.00",@"-2.25",@"-2.50",@"-2.75",@"-3.00",@"-3.25",@"-3.50",@"-3.75",@"-4.00",@"-4.25",@"-4.50",@"-4.75",@"-5.00",@"-5.25",@"-5.50",@"-5.75",@"-6.00",@"-6.25",@"-6.50",@"-6.75",@"-7.00",@"-7.25",@"-7.50",@"-7.75",@"-8.00",@"-8.25",@"-8.50",@"-8.75",@"-9.00",@"-9.25",@"-9.50",@"-9.75",@"-10.00",@"-10.25",@"-10.50",@"-10.75",@"-11.00",@"-11.25", @"-11.50",@"-11.75",@"-12.00", nil];


from this array i want to add values from -4.25 to -8.50 to another array like

SpositvSph = [NSArray arrayWithObjects:@"-4.25",@"-4.50",@"-4.75",@"-5.00",@"-5.25",@"-5.50",@"-5.75",@"-6.00",@"-6.25",@"-6.50",@"-6.75",@"-7.00",@"-7.25",@"-7.50",@"-7.75",@"-8.00",@"-8.25",@"-8.50", nil];


can anyone help me out?

Answer

You can use subarrayWithRange: with NSArray like this.

NSInteger startIndex = [SnegatvSph indexOfObject:@"-4.25"];
NSInteger toIndex = [SnegatvSph indexOfObject:@"-8.50"];
if (startIndex < SnegatvSph.count && toIndex < SnegatvSph.count) {
    NSArray *newArray = [SnegatvSph subarrayWithRange:NSMakeRange(startIndex, (toIndex - startIndex + 1))];
    NSLog(@"%@",newArray);
}
Comments