0

I have NSMutableArray named dishArray. I have total 15 objects in this array.

I only want first three objects in array and delete the rest array.

Is there any way (other then looping) to delete?

I know using loop I can achieve it, but I am looking for alternate way. May be like deleteArray From: To:

5 Answers 5

2
NSMutableArray *array = ...;
if ([array count] > 3) {
    [array removeObjectsInRange:NSMakeRange(3, [array count] - 3)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1: for condition, else app will crash & thanks, will accept after 10 mins...
1

Use the function removeObjectsInRange.

 if ([yourArray count] > 3)
 [yourArray removeObjectsInRange:NSMakeRange(3, [yourArray count] - 3)];

1 Comment

And if the array only contains 1 object?
1

Try with following code:

 if ([wholeArray count] > 3)    
    NSArray* finalArray = [wholeArray removeObjectsInRange(2, wholeArray.count-3)];

Comments

0
    NSRange r;
    r.location = 0; // start position
    r.length = 3;  // end position

    [arr removeObjectsInRange:r];

Comments

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

1 Comment

Please explain your answer in brief

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.