0

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'

2
  • Have you tried to init your NSMutableArray without a capacity? It is not required and that may assist. Commented Apr 15, 2015 at 20:42
  • Why are you "insertObject" on reverseString? You're already going through the list backwards, you should just be addObject. Commented Apr 15, 2015 at 20:43

3 Answers 3

3

[reverseString insertObject:inputString[i] atIndex:i]; is the problem - specifically the atIndex:i part. You've created your mutable array with a large enough capacity, however it is still empty so specifying the index here is not what you meant to do.

Instead, just use addObject: [reverseString addObject:inputString[i]];

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

Comments

2

Try this:

for (int i = lenArray-1; i >= 0; i--) {
    [reverseString addObject:inputString[i]];
}

Note: Just use addObject: and change your for-loop condition to i >= 0, otherwise the "a" will be missing.

Enjoy!

2 Comments

Hi Jonahnnes, system does not allow me add another question. Sorry for bothering. I wanted to print the reverseString and typed the following code` for(int i=0;i<=lenArray-1;i++){printf("%s\n",[reverseString objectAtIndex:i]);}return 0;}}` but it prints the weird characters.
No problem. You are dealing with NSStrings. These are different from C strings. So you need to use different methods to print the string. Try either NSLog(@"%@", reverseString); or (in your for-loop) NSLog(@"%@\n", [reverseString objectAtIndex:i]);
0

Here is the complete solution:

#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];
      //  NSArray* reversedArray = [[inputString reverseObjectEnumerator] allObjects];

            int lenArray= (int)[inputString count];

            NSMutableArray *reverseString=[[NSMutableArray alloc]initWithCapacity:lenArray];

                for (int i = lenArray-1; i >= 0; i--) {
                    [reverseString addObject:inputString[i]];
                }

               for(int i=0;i<=lenArray-1;i++)
                {
                    NSLog(@"%@\n", [reverseString objectAtIndex:i]);
                }
    return 0;
  }
}

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.