Search code examples
iphoneios6

[__NSArrayI objectAtIndex:]: index 8 beyond bounds [0 .. 7]'


I'm new to iOS development and as I follow a tutorial my app continues to crash but I cant see why..

2012-11-07 11:52:30.657 SliderList[2725:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 8 beyond bounds [0 .. 7]'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c42b44 0x2cb2 0x10de705 0x15920 0x158b8 0xd6671 0xd6bcf 0x15c52d 0xd5968 0x451bd 0x45552 0x233aa 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x1be77e3 0x1be7668 0x1265c 0x26bd 0x25e5 0x1)
libc++abi.dylib: terminate called throwing an exception

 #import "ViewController.h"
   NSArray *keuzeArray;

   @interface ViewController ()

   @end

   @implementation ViewController
   @synthesize keuze;
   - (IBAction)sliderValueChanged: (UISlider *)sender
   {
self.keuze.text = [keuzeArray objectAtIndex:sender.value];
   }

   - (void)viewDidLoad
   {
[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

keuzeArray = [NSArray arrayWithObjects:
              @"Koffie zwart", @"Koffie melk", @"Koffie melk&suiker", @"cafe au lait",               @"Koud water", @"Heet water", @"Chocomel", @"Wiener melange", nil];
   }
   - (void) viewDidUnload
   {
[super viewDidUnload];
   }

   - (BOOL) shouldAutorotateToInterfaceOrientation:
   (UIInterfaceOrientation)InterfaceOrientation
   {
return (InterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
   }

   @end

Solution

  • You are calling a object from your array that the index is not on your array.

    Basically your array has 8 elements (0 to 7) and you are calling the index 8

    Just change this line:

    self.keuze.text = [keuzeArray objectAtIndex:(sender.value - 1)];
    

    and it all should work