为什么这个for循环不执行?

Ste*_*eve 2 iphone xcode loops objective-c uipickerview

我有一个选择器视图控制器来选择化学源和可能的浓度.如果光源没有浓度,它只会提供一个选择器.它由一个NSDictionary源类型名称keys和一个我调用的自定义模型对象填充,Chemical它有四个属性,两个NSString,一个float和一个BOOL.

当我用包含2个组件的字典触发它时,我想从Chemical表示的那个中提取四个值.请注意,我使用前两个属性的值填充选择器,但不包括floatBOOL.我遍历数组中的第一个组件中选择的键,并检查第二个组件中的字符串与键/值数组中chemConcentration每个Chemicals 的属性.当chemConcentration比赛时,我知道我有权利Chemical,我可以将其属性发回.

呼!

问题是,即使我知道我进入for循环,它似乎被跳过了.NSLog在它打印之前的右边,但里面的那个没有. sourceConstantsourceIsLiquid0.0NO

- (IBAction)selectedSourceButton {
    NSLog(@"selectedSourceButton pressed");
    NSInteger sourceRow = [picker selectedRowInComponent:kSourceComponent];
    NSString *selectedSource = [self.sources objectAtIndex:sourceRow];
    NSArray *selectedChemicalGroup = [dictionaryOfSources objectForKey:selectedSource];
    NSInteger concentrationRow = [picker selectedRowInComponent:kConcentrationComponent];
    NSString *selectedConcentration = [[NSString alloc] init];
    float selectedConstant = 0.0;
    BOOL selectedIsLiquid = NO;

    if (numberOfComponents == 2) {
        NSLog(@"numberOfComponents = 2 if/then chosen"); // <-- This prints.
        selectedConcentration = [self.concentrations objectAtIndex:concentrationRow];
        NSLog(@"begin selectedConcentration for loop.  Number of loops = %d", [selectedChemicalGroup count]); // <-- And so does this.
        for (int i; i<[selectedChemicalGroup count]; i++) { // <-- But this doesn't seem to fire!
            NSLog(@"selectedConcentration = %@, from selectedChemicalGroup = %@", selectedConcentration, [[selectedChemicalGroup objectAtIndex:i] chemConcentration]); // <-- Because this doesn't print.
            if ([selectedConcentration isEqualToString:[[selectedChemicalGroup objectAtIndex:i] chemConcentration]]) {
            selectedConstant = [[selectedChemicalGroup objectAtIndex:i] chemConstant];
            selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:i] chemIsLiquid];
            }
        }
    }
    else {
        selectedConcentration = @"";
        selectedConstant = [[selectedChemicalGroup objectAtIndex:0] chemConstant];
        selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:0] chemIsLiquid];
    }
    NSLog(@"selectedSourceButton source to return = %@, concentration = %@, sourceConstant = %1.7f, isLiquid = %d", selectedSource, selectedConcentration, selectedConstant, selectedIsLiquid);
    if ([self.delegate respondsToSelector:@selector (sourcePickerViewController:didSelectSource:andConcentration:andConstant:andIsLiquid:)]) {
        [self.delegate sourcePickerViewController:self didSelectSource:selectedSource andConcentration:selectedConcentration andConstant:selectedConstant andIsLiquid:selectedIsLiquid];
    }
}
Run Code Online (Sandbox Code Playgroud)

jtb*_*des 6

您需要初始化变量i:for (int i = 0; ...

但是使用"快速枚举"有更好的方法:

for (MyChemicalGroupClass *group in selectedChemicalGroup) {
    if ([selectedConcentration isEqualToString:[group chemConcentration]]) {
    ...
    }
}
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 5

初始化循环计数i

for (int i = 0; i<[selectedChemicalGroup count]; i++)
Run Code Online (Sandbox Code Playgroud)