- (IBAction)reverseMethod
{
//NSUInteger count = [array1 count];
for( int i=[array1 count]-1;i<[array1 count];i--)
{
[array2 addObject:[array1 objectAtIndex:i]];
NSLog (@"Object at index %d is: %@",
i, [array1 objectAtIndex: i]);
}
array1=array2;
[tblMyTable reloadData];
}
Asked
Active
Viewed 41 times
0

EmptyStack
- 51,274
- 23
- 147
- 178

user983398
- 1
- 1
-
1What you are doing using the above code? – EmptyStack Oct 13 '11 at 11:37
-
@user983398 When you want to terminate the loop? (why this condition i<[array1 count]) , i think you need check i>0 . Answer of Empty stack does what you are trying in your unmanaged code. – Ishu Oct 13 '11 at 12:00
2 Answers
1
If you are trying to reverse the objects of an array, you do it like this,
- (IBAction)reverseMethod {
array1 = [[array1 reverseObjectEnumerator] allObjects];
[tblMyTable reloadData];
}

EmptyStack
- 51,274
- 23
- 147
- 178
0
You are not clearing the array2 instance variable upon entering the reverseMethod. Every time you enter the reverseMethod the array1 objects are added again.
Either clear the array2 when entering the reverseMethod or define array2 locally within reverseMethod.
The elements are not reversing because the for-loop runs only while i<[array1 count]
it should run until i > 0
. You want to iterate from the last index of array1 to the first index, that being zero.
Also see this answer for a great example of reversing an NSArray.

Community
- 1
- 1

Niels Castle
- 8,039
- 35
- 56