views:

110

answers:

3

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];
    }
}
+5  A: 

Initialize loop count i

for (int i = 0; i<[selectedChemicalGroup count]; i++)
Vladimir
+2  A: 

Do the following and you will understand why:

int i;
NSLog(@"%d", i);
beefon
+6  A: 

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]]) {
    ...
    }
}
jtbandes
+ for fast enumeration
Vladimir