2010-07-25 92 views
2

我有一個選擇器視圖控制器來選擇一個化學源和可能的濃度。如果來源不具有濃度,則只顯示一個選擇器。它由NSDictionary填充,源類型名稱爲keys,我製作的自定義模型對象Chemical有四個屬性,兩個NSString,1個float和一個BOOL爲什麼不執行循環?

當我用包含2個組件的字典觸發此操作時,我想提取表示的Chemical中的四個值。請注意,我使用前兩個屬性的值填充選取器,但不是floatBOOL。我在第一個組件中選中的鍵的數組中運行數組,並檢查第二個組件中的字符串與鍵/值數組中的每個Chemicals的chemConcentration屬性。當chemConcentration匹配時,我知道我有正確的Chemical,我可以獲取其屬性發回。

Whe!

問題是,即使我知道我到達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]; 
    } 
} 

回答

5

初始化循環計數我

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

執行以下操作,你就會明白爲什麼:

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

需要初始化你的變量ifor (int i = 0; ...

但有一個更好的方法來做到這一點,使用「快速枚舉」:

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

+ for fast enumeration – Vladimir 2010-07-25 20:45:21