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

发布于 2024-09-11 16:44:15 字数 2998 浏览 5 评论 0原文

我有一个选择器视图控制器来选择化学源和可能的浓度。如果源没有集中,它只显示一个选择器。它由一个 NSDictionary 填充,其中源类型名称为 keys 以及我制作的名为 Chemical 的自定义模型对象,该对象具有四个属性,两个 >NSString,一个 float 和一个 BOOL

当我使用具有 2 个组件的字典触发此操作时,我想从所表示的 Chemical 中提取四个值。请注意,我使用前两个属性的值填充选取器,而不是 floatBOOL。我遍历第一个组件中选择的键的数组,并根据键中每个 ChemicalchemConcentration 属性检查第二个组件中的字符串/值数组。当 chemConcentration 匹配时,我知道我拥有正确的 Chemical 并且我可以获取其属性并发回。

哇!

问题是,尽管我知道我进入了 for 循环,但它似乎被跳过了。 NSLog 就在它打印之前,但里面的却没有打印。 sourceConstantsourceIsLiquid 保持 0.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];
    }
}

I have a picker view controller to select a chemical source and possibly a concentration. If the source doesn't have concentrations, it just presents a single picker. It gets populated by an NSDictionary with source type names as keys and a custom model object I made called Chemical that has four properties, two NSString, one float and one BOOL.

When I trigger this with dictionary that has 2 components, I want to extract the four values from the Chemical that is represented. Note that I populate the picker with values from the first two properties, but not the float or BOOL. I run through the array for the key that's selected in the first component and check the string from the second component against the chemConcentration property from each of the Chemicals in the key/value array. When the chemConcentration matches, I know I have the right Chemical and I can get its properties to send back.

Whew!

The problem is that even though I know I get to the for loop, it seems to get skipped. The NSLog right before it prints, but the one inside doesn't. sourceConstant and sourceIsLiquid stay 0.0 and NO

- (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];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-09-18 16:44:16

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

但有更好的方法来做到这一点,使用“快速枚举”:

for (MyChemicalGroupClass *group in selectedChemicalGroup) {
    if ([selectedConcentration isEqualToString:[group chemConcentration]]) {
    ...
    }
}

You need to initialize your variable i: for (int i = 0; ...

But there's a better way to do this, using "fast enumeration":

for (MyChemicalGroupClass *group in selectedChemicalGroup) {
    if ([selectedConcentration isEqualToString:[group chemConcentration]]) {
    ...
    }
}
街角迷惘 2024-09-18 16:44:16

初始化循环计数 i

for (int i = 0; i<[selectedChemicalGroup count]; i++)

Initialize loop count i

for (int i = 0; i<[selectedChemicalGroup count]; i++)
狠疯拽 2024-09-18 16:44:16

执行以下操作,您就会明白原因:

int i;
NSLog(@"%d", i);

Do the following and you will understand why:

int i;
NSLog(@"%d", i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文