Reputation:
I have the following code and am getting error at NSString *weight line:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray objectAtIndex:]: index 2 beyond bounds [0 .. 1]' - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
NSArray *array;
array = [[NSArray alloc]initWithObjects:@"0", @"1/2", nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
NSString *weight = [[array objectAtIndex:row] stringValue];
label.text = [NSString stringWithFormat:@"%@", weight]
Upvotes: 0
Views: 222
Reputation: 57179
You have 2 objects in your array and it looks like from your exception that row is 2 which will try to access a 3rd object. In your situation where you have only 2 objects declared in the same method why not use a switch statement and log/assert on the default case? Ultimately you need to make sure you tell the picker the right amount of columns for the right component.
Upvotes: 0
Reputation: 19656
The value of "row" is 2, but your array only has 2 elements, meaning the maximux value of row should only ever be "1"
Upvotes: 0
Reputation: 71048
The problem is right there in the error message. What's wrong is that you have an array with 2 things in it (the strings "0" and "1/2"), and you're asking it for index 2, which is invalid. (Only 0 and 1 would be valid).
You don't show how you're getting row
but that's your problem. It's too big.
Upvotes: 2