How to increment a 'score' during fast enumeration
-
01-06-2021 - |
Question
I have the following code which I would like to use to check user answers and output a score (out of 5). I use a plist with the answers in and check the textField.text against it. What I'm struggling with is: how to get an output score as a total using this method?
- (IBAction)checkAnswers:(UITextField *)textField
{
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"7A Cells Microscopes 3" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path2];
NSString *tester = [NSString stringWithFormat:@"%i", textField.tag];
// NSDictionary *secondDict = [dictionary valueForKey:tester];
// NSString *answer = [secondDict valueForKey:@"Answer"];
// if ([textField.text isEqualToString:[[dictionary valueForKey:tester] valueForKey:@"Answer"]]) {
// NSLog(@"YAY");
// }
NSArray *allTextFields = [[NSArray alloc] initWithObjects:eyepiece, objectiveLens, focussingKnobs, stage, mirror, nil];
for (textField in allTextFields) {
int x = 0;
if ([textField.text isEqualToString:[[dictionary valueForKey:tester] valueForKey:@"Answer"]]) {
x++;
NSLog(@"%i", x);
}
}
Any help would be much appreciated! Many thanks.
Solution
Assuming the rest of your code is good, just move int x = 0; outside the for loop. The way you have it coded x is reset to 0 on every loop... so it never counts.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow