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

标签 iphone objective-c xcode loops uipickerview

我有一个选择器 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/

相关文章:

xcode - 无法启动 IBSimDeviceTypeiPad2x

ios - Xcode 不支持架构 armv7k

ios - 需要通过 MoPub 展示 Admob 广告

iphone - 在 iPad 上显示 PDF 文档 - 颜色问题

ios - 将NSInteger和NSString转换为字节数组

objective-c - Xcode Objective-C : Why does UIImage imageNamed method reset all other images to default locations?

objective-c - 如何处理在 Obj C 或 Swift 中返回对象或 nil 的 Core Data 自定义方法

ios - 如果框架未链接到项目,则发出警告

iphone - iOS 5崩溃@ [self.navigationController pushViewController:vcSummary动画:是];

iphone - 设置 map View 中心坐标后,自定义注释 View 将从其 super View 中删除