iOS - Wierd out of bound exception

I have a pickerView with some strings in it (0,1,2,3,4,5,6,7) and in the other component (0,15,30,45)

everything is working fine except, when i choose something from component 1, above 3. it gives me an out of bounds exception.

in viewDidLoad i declare it and fill it.

hoursArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7"];

i do not touch it at all, only for nslogs. in the pickers didSelectRow, i can NSlog([hoursarray objectAtindex:7) and it writes 7.

but if i pick above 3 in the picker, it gives me:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 3]

the following code is a NSLOG to print array index 4, its working. but in the picker index 4 is out of bounds, and it IS the same array

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

NSLog(@"hours array index 4:%@",[hoursArray objectAtIndex:7]);


NSString *selectedHours = @"0";
NSString *selectedQuarters = @"0";

//get picker values
if(component==0) {
    selectedHours = [hoursArray objectAtIndex:row];
    selectedQuarters = [quarterArray objectAtIndex:row];
}else {
    selectedHours = [hoursArray objectAtIndex:row];
    selectedQuarters = [quarterArray objectAtIndex:row];
}
//create formatted date
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@" dd/MM"];
NSString *displayDate = [formatter stringFromDate:[datePicker date]];

NSString *lblString = [[NSString alloc]initWithFormat:@"Tryk for at tilføje %@ time(r) & %@ minutter til d. %@ ",selectedHours,selectedQuarters,displayDate];

[btnRegistrer setTitle:lblString forState:UIControlStateNormal];

}

Upvotes: 1

Views: 1084

Answers (2)

Mani
Mani

Reputation: 17585

I'm trying to write clear code .. It may help you..

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component{

  NSLog(@"hours array index 4:%@",[hoursArray objectAtIndex:7]);

  //get picker values
 if(component==0) {
    self.selectedHours = [hoursArray objectAtIndex:row];
   }else if(component==1){
    self.selectedQuarters = [quarterArray objectAtIndex:row];
   }

  [self showButtonTitle];
}
-(void)showButtonTitle
{
   //create formatted date
  NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  [formatter setDateFormat:@" dd/MM"];
  NSString *displayDate = [formatter stringFromDate:[datePicker date]];

  NSString *lblString = [[NSString alloc]initWithFormat:@"Tryk for at tilføje %@ time(r) &     %@ minutter til d. %@     ",self.selectedHours,self.selectedQuarters,displayDate];

   [btnRegistrer setTitle:lblString forState:UIControlStateNormal];
}

Upvotes: 0

LombaX
LombaX

Reputation: 17364

You have the same code in both statements of the if!! It should be:

if(component==0) {
    selectedHours = [hoursArray objectAtIndex:row];
}else {
    selectedQuarters = [quarterArray objectAtIndex:row];
}

EDIT: I answered before seeing your last edit with the complete delegate method, moreover you have to change a bit the logic because, if the user changes both hours and quarters, you'll have TWO subsequent delegate calls, and you'll have to adjust selectedHours and selectedQuarters. You'd better move these two variables inside a @property to accomplish this

Upvotes: 1

Related Questions