I would like to reverse an array of string in Objective-C.
I know there is a in built-method of doing it as follows:
NSArray* reversedArray = [[inputString reverseObjectEnumerator] allObjects];
but I want to implement same functionality with coding as follows and getting the following error.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *inputString = [NSMutableArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", nil];
int lenArray= (int)[inputString count];
NSMutableArray *reverseString=[[NSMutableArray alloc]initWithCapacity:lenArray];
for(int i=lenArray-1;i>0;i--)
{
[reverseString insertObject:inputString[i] atIndex:i];
}
return 0;
}
}
Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM insertObject:atIndex:]: index 8 beyond bounds for empty array'
NSMutableArraywithout a capacity? It is not required and that may assist.