I am trying to simplify a switch-statement that takes a lot of space and time to write. Se the code, you’ll understand. If not, I’ll explain later.
// When row is selected
- (void)pickerView:(UIPickerView *)pickerTimer didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (row) {
case 0:
NSLog(@"It obviously worked.0");
break;
case 1:
NSLog(@"It obviously worked.1");
break;
case 2:
NSLog(@"It obviously worked.2");
break;
case 3:
NSLog(@"It obviously worked.3");
break;
case 4:
NSLog(@"It obviously worked.4");
break;
case 5:
NSLog(@"It obviously worked.5");
break;
case 6:
NSLog(@"It obviously worked.6");
break;
default:
NSLog(@"It did kindof work.NIL");
break;
}
}
Is there any way of simplifying this? Just do like,
NSLog(@"It did work! %@", row);
For the record, I tried that, and it did not work.
The format specifier for an integer is
%d—%@specifies an object. To be safe, you should also cast the NSInteger to anintwhen you pass it toprintf()(so you would write@"%d", (int)row), because the size of NSInteger is not guaranteed to be the size the%dspecifier tellsprintf()to expect.Incidentally, you don’t need to repeat it for each case statement. Without a break, control will fall through to the next case.