2012-11-07 39 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; 
} 
相關問題