gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:19:17 27 4
gpt4 key购买 nike

我有一个选择器 View Controller 来选择化学源和可能的浓度。如果源没有集中,它只显示一个选择器。它由一个 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:for (int i = 0; ...

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

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

关于iphone - 为什么这个for循环不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3330857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com