2012-11-07 41 views
-1

我新的iOS开发和我跟着一个教程我的应用程序继续崩溃,但我不能明白为什么..[__NSArrayI objectAtIndex:]:指数8超出范围[0..7]”

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 

回答

0

您正在从数组中调用索引不在数组中的对象。

基本上你的数组有8个元素(0 to 7),您呼叫的指数8

只是改变这一行:

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

而这一切都应该工作

0

你只有8元素。 在数组中,索引从0开始。因此数组中的最后一个元素索引是7。 如果您尝试获取索引为8或以上的元素,则会发生异常(NSRangeException),并且您的应用会崩溃。 因此,当您尝试从数组中获取元素时,请检查数组边界。

1

滑块的最大值超出了模型数组中元素的数量。

1

看起来sliderValueChanged:方法中的UISlider传递值为8。由于NSArray中的索引是从零开始的,因此您不能要求超出7的元素。

您需要重新配置滑块,将最小值设置为0,并将最大值设置为7

0

您的数组有8个对象,索引为0到7. 而您试图访问索引为8的对象。这就是为什么它会抛出异常错误。

0

如果它在的UITableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrayResponse.count-1; 
} 
相关问题