For Loop to do repetitive checking of variables for me
NSArray *test = [NSArray arrayWithObjects:@"22", @"3", @"22", @"5", @"1", @"0", @"2", nil];
NSArray *test2 = [NSArray arrayWithObjects:@"21", @"2", @"20", @"5", @"1", @"9", @"2", nil];
for(int i = 0; i < 7; i++) {
if ([test objectAtIndex:i] == [test2 objectAtIndex:i]); {
testVariable = testVariable + 1;
}
}
NSLog(@"%i", testVariable);
I tried the above code to test comparison of variables but it returns 7 when it should return 3. Do I need to somehow retrieve and store each array object in a local variable and compare thos against each other? Or can I do something more direct like what I tri开发者_StackOverflow社区ed above. Arrays are very interesting. :)
UPDATE:
Got it to work with NSInteger.. :) Guess I was comparing objects and not the actual integer numbers before..
You can use preprocesor
#define variable(name,number) {name##number}
and later in the loop
for (int a = 1; a <= 53; a++) {
if ((variable(taken,a) == 2) && (variable(hidden,a) == 2)) {
//Do something
}
}
The simple answer is not to use different variables. Use a collection or an array instead. Then you could have:
if (taken[a] == 2 && hidden[a] == 2) {
...
}
Think of using an array whenever you find yourself putting numeric suffixes on variables of the same type and prefix.
精彩评论