0

I want to take the values from NSMutableArray but want to read from last index to 1st index

thank you

2
  • Have you tried for ( int i = [array count]-1; i>=0; i--) ? Commented Apr 27, 2011 at 15:58
  • Otherwise, try one of these: – sortedArrayUsingDescriptors: – sortedArrayUsingSelector: – sortedArrayUsingComparator: Commented Apr 27, 2011 at 16:00

3 Answers 3

5
for (id someObject in [someArray reverseObjectEnumerator])
{
    //do your thing
}
Sign up to request clarification or add additional context in comments.

Comments

2

2 other options:

Simple for-loop (surely not recommended):

for (int i = [array count]-1; i >= 0; ++i)
    id value = [array objectAtIndex: i];

Block-based enumeration:

[array enumerateObjectsWithOptions: NSEnumerationReverse
                        usingBlock: ^(id obj, NSUInteger idx, BOOL *stop){
                              //do something
                        }];

Comments

1

Mark's answer is useful, but this form may be useful when you want to mutate the array:

while ([arr count]) {
  id obj  = [arr lastObject];
  // use obj
  [arr removeLastObject];
}

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.