Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop will run once at most, (loop increment never executed)

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;
    }
}
like image 438
Benr783 Avatar asked Nov 30 '22 19:11

Benr783


1 Answers

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.

like image 147
nhgrif Avatar answered Dec 21 '22 11:12

nhgrif