2010-07-25 6 views
2

필자는 화학 소스와 농도를 선택할 수있는 선택기 뷰 컨트롤러를 가지고 있습니다. 소스에 농도가 없으면 단일 피커가 나타납니다. 소스 형식 이름이 NSDictionary이고 채우기 유형이 keys이고 사용자 지정 모델 개체가 Chemical이고 두 속성이 NSString이고 하나가 float이고 하나가 BOOL 인 채우는 사용자 지정 모델 개체로 채워집니다.this for for 루프가 실행되지 않는 이유는 무엇입니까?

두 개의 구성 요소가있는 사전을 사용하여이를 트리거하면 표현 된 Chemical에서 네 개의 값을 추출하고 싶습니다. 피커에 처음 두 속성의 값을 채우지 만 float 또는 BOOL이 아닌 값을 채 웁니다. 첫 번째 구성 요소에서 선택된 키에 대한 배열을 실행하고 키/값 배열의 Chemical 각각의 chemConcentration 속성에 대해 두 번째 구성 요소의 문자열을 확인합니다. 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]; 
    } 
} 

답변

5

초기화 루프 카운트 내가에게

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

는 다음 작업을 수행하고 그 이유를 이해할 수있을 것이다 : for (int i = 0; ... : 당신은 당신의 변수 i를 초기화해야

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

"빠른 열거 형"을 사용하여 더 나은 방법을 사용할 수 있습니다 :

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

+ 빠른 열거 형 – Vladimir

관련 문제