2012-03-19 3 views
0

동일한 루프에서 두 개의 다른 배열에서 값을 계산하고 선택하는 방법은 무엇입니까? 그텍스트 필드를 반복하는 방법 두 가지 다른 NSMUTABLEARRAY에서 인덱스는 동일합니다

예를 들어 같은

뭔가 :

for(UITextField *field in textFieldArray && textFieldArray2) 

은 내가 값을 뺄하고 난 두 배열을 가지고 있고 나는 다른 배열하지만 같은 인덱스에서이 텍스트 필드의 값을 빼 할 ...

내 코드는

int result = 0; 
for(UITextField *field in textFieldArray) // will iterate all UITextField which was added 
{ 
    result += [field.text intValue]; //or floatValue 
} 
NSLog(@"the result is %d", result); //here you have sum of all the text fields' text 

내가이 빼기 싶지만 두 텍스트 필드

int y12 = ([Txt_New_Actual.text intValue]); 
float c116 = y12-([Txt_New_Estimated.text floatValue]); 
Txt_New_ONU.text = [[NSString alloc] initWithFormat:@"%.2f",c116]; 

답변

0

빠른 열거 루프에서 현재 개체의 인덱스를 가져온 다음 indexOfObject:을 사용하여 인덱스를 가져와 두 번째 배열에서 objectAtIndex:과 함께 사용합니다. 이 같은

뭔가 : 2 배열 반환 값이 유효한 경우

int result = 0; 
for(UITextField *field in textFieldArray) 
{ 
    int indexOfObject = [textFieldArray indexOfObject:field]; 
    UITextField *secondValue = (UITextField *)[textFieldArray objectAtIndex:indexOfObject]; 
    result += [secondValue.text intValue]; 
} 
NSLog(@"the result is %d", result); 

당신은 아직도 그 코드에 검사의 일종을 넣어해야합니다. 또 다른 솔루션을 사용하여 루핑

+0

대단히 감사합니다 alooot .... .... gr8888 도움말 ... –

0

중 하나 enumerateWithBlock를 사용하거나 루프에서 증가 변수 인덱스가 ... 다른 배열에 있지만 같은 인덱스가 있습니다. objectAtIndex :를 사용하여 다른 배열에 액세스하십시오.

+0

감사 –

0

대신 foreach는 절, 예를 들어

int result = 0; 
int count = [textFieldArray count]; 
for(int i=0; i<count; i++) 
{ 
    UITextField field = [textFieldArray objectAtIndex:i]; 
    int y12 = ([field.text intValue]); 
    field = [theOtherArray objectAtIndex:i]; 
    float c116 = y12-([field.text floatValue]); 
} 
+0

대단히 감사합니다 .... gr8888 도움 .... –

+0

아무런 probs, 내가 도울 수있어서 기쁩니다. 문제가 해결 된 경우 답변 중 하나를 답으로 표시하는 것을 고려해보십시오 (반드시 광산 일 필요는 없음). – Amiramix

관련 문제