I have a picker view and I want the the things in it to be the tail numbers of the planes. I use this code, but I get the warning Loop will run once at most, (loop increment never executed)
, and then I get the error control may each end of non-void function.
What is wrong here?
CODE:
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
for (NSInteger i = [[NSUserDefaults standardUserDefaults]integerForKey:@"planeNum"]; (i = 0); i--) {
NSString *dTailNumber = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"Plane%liTailNumber", i]]];
return dTailNumber;
}
}
The reason you get the warning
Loop will run once at most, (loop increment never executed)
Is because you have an unconditional return
in the body of the loop. On the first execution of the loop, it will return
out of the method before the loop increment is reached.
The reason you get the
control may reach the end of a non-void function
is because there's no guarantee for this function to ever return anything. What if the loop executes 0 times? Then the method never returns anything, which isn't okay since it's declared as returning an NSString
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With