5

I have 1 mutablearray, i want to remove object all indexs but hold object at first index. EXample : Input : Array (a,b,c,d,e) Output: Array (a)

Can you help me. Thanks in advance

1
  • 3
    Did you have a look at the NSMutableArray documentation before asking this question? Commented Jul 2, 2013 at 9:06

3 Answers 3

19
NSMutableArray *testArray=[[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C", nil];
[testArray removeObjectsInRange:NSMakeRange(1, testArray.count-1)];

But please make sure array count is greater than 1.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this one and put your range to remove your object.

[array removeObjectsInRange:NSMakeRange(1, array.count-1)];

Comments

1

Keep a reference to your a object (MyObject temp = [array objectAtIndex:0]), do removeAllObjects, then insertObject:a

1 Comment

This is a very bad practice since NSMutableArray class has specific APIs to do so. You must read API's documentation or even the class header file to get a better approach.

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.