问题描述
我有一个选择器视图,我希望其中的东西是飞机的尾号.我使用了这段代码,但是我得到警告 Loop最多将运行一次,(循环增量将永远不会执行)
,然后我收到错误消息 control可能会在每个非void函数的结尾.
这是怎么了?
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?
代码:
- (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;
}
}
推荐答案
收到警告的原因
是因为循环的主体中有无条件的 return
.第一次执行循环时,它将在达到循环增量之前返回
退出该方法.
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.
您获得
是因为无法保证此函数会返回任何内容.如果循环执行0次该怎么办?然后该方法从不返回任何内容,这是不正确的,因为它被声明为返回 NSString
.
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
.
这篇关于循环最多运行一次(循环增量从未执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!