3

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?

3
  • Use NSMutableArray to insertObjectAtIndex Commented Sep 9, 2016 at 6:40
  • NSMutableArray *arr = [NSMutableArray arrayWithArray:SpositvSph]; [arr insertObject:@"someValue" atIndex:YOURINDEX]; Commented Sep 9, 2016 at 6:41
  • NSArray is static and NSMutableArray is dynamic means NSArray is only init time add but NSMutableArray is any time add. Commented Sep 9, 2016 at 6:43

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.