简体   繁体   English

如何从两个不同的NSMUTABLEARRAY迭代文本字段,但索引相同

[英]How to iterate textfield From Two Different NSMUTABLEARRAY But index are same

How to calculate and pick the value from two diffrent array on a same loop? 如何在同一循环中从两个差分数组计算并选择值?

something like that 这样的东西

eg: 例如:

for(UITextField *field in textFieldArray && textFieldArray2)

i have to subtract the value and i have 2 Array and i want to subtract the value of two textfield from different array but same index... 我必须减去值并且我有2个数组,我想从不同的数组但相同的索引中减去两个文本字段的值...

My code is 我的代码是

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

i want to subtract this but both text fields are on different Array but have same index... 我想减去这个,但两个文本字段都在不同的数组上,但是具有相同的索引...

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

Either use the enumerateWithBlock or have a variable index that you increment in the loop. 可以使用enumerateWithBlock或具有在循环中递增的变量索引。 Access the other array with objectAtIndex:. 使用objectAtIndex:访问另一个数组。

Get the index of the current object in your fast enumeration loop, then use indexOfObject: to get the index and use that with objectAtIndex: on your second array. 在快速枚举循环中获取当前对象的索引,然后使用indexOfObject:获取索引并将其与第二个数组上的objectAtIndex:一起使用。

Something like this: 像这样:

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); 

You should still put some kind of check into that code if the returned value from the 2nd array is valid. 如果第二个数组返回的值有效,您仍然应该对代码进行某种检查。

Another solution is looping using a for clause instead of foreach, eg: 另一种解决方案是使用for子句而不是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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM