If-else statement not evaluating

Discussion in 'iOS Programming' started by alexatwater, Feb 9, 2014.

  1. alexatwater macrumors newbie

    Joined:
    Feb 9, 2014
    #1
    This is driving me nuts. This is a “mocked up” version of a major issue i’m having in my actual project and is really frustrating me, any help would be so much appreciated. So you can copy this exact code into a blank command line app and see my issue:

    Code:
    NSMutableDictionary *loadGradeDictionary = [[NSMutableDictionary alloc] initWithCapacity:11];
            [loadGradeDictionary setObject:[NSArray arrayWithObjects:@"A", @"A-", @"B+", @"B", @"B-", @"C+", @"C", @"C-", @"D+", @"D", @"D-", @"F" , nil] forKey:@"gradesArray"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (92.5 + 0.5)] forKey:@"A"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (89.5 + 0.5)] forKey:@"A-"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (86.5 + 0.5)] forKey:@"B+"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (82.5 + 0.5)] forKey:@"B"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (79.5 + 0.5)] forKey:@"B-"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (76.5 + 0.5)] forKey:@"C+"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (72.5 + 0.5)] forKey:@"C"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (69.5 + 0.5)] forKey:@"C-"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (66.5 + 0.5)] forKey:@"D+"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (62.5 + 0.5)] forKey:@"D"];
            [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (59.5 + 0.5)] forKey:@"D-"];
            NSLog(@"\n%@", loadGradeDictionary);
    
    
            float passedGrade = 90;
            NSDictionary *gradeScale = loadGradeDictionary;
            NSMutableString *returnString = [[NSMutableString alloc] initWithCapacity:2];
            NSString *xString = [NSString stringWithFormat:@"%.2f", passedGrade];
    
            if (([xString floatValue] > [[gradeScale valueForKey:@"A"] floatValue])) {
                //return the letter here
                returnString = [@"A" mutableCopy];
            } else if (([[gradeScale valueForKey:@"A"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"A-"] floatValue])) {
                //return the letter here
                returnString = [@"A-" mutableCopy];
                //return @"A-";
            } else if (([[gradeScale valueForKey:@"A-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B+"] floatValue])) {
                //return the letter here
                returnString = [@"B+" mutableCopy];
                //return @"B+";
            } else if (([[gradeScale valueForKey:@"B+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B"] floatValue])) {
                //return the letter here
                returnString = [@"B" mutableCopy];
                //return @"B";
            } else if (([[gradeScale valueForKey:@"B"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B-"] floatValue])) {
                //return the letter here
                returnString = [@"B-" mutableCopy];
                //return @"B-";
            } else if (([[gradeScale valueForKey:@"B-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C+"] floatValue])) {
                //return the letter here
                returnString = [@"C+" mutableCopy];
                //return @"C+";
            } else if (([[gradeScale valueForKey:@"C+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C"] floatValue])) {
                //return the letter here
                returnString = [@"C" mutableCopy];
                //return @"C";
            } else if (([[gradeScale valueForKey:@"C"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C-"] floatValue])) {
                //return the letter here
                returnString = [@"C-" mutableCopy];
                //return @"C-";
            } else if (([[gradeScale valueForKey:@"C-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D+"] floatValue])) {
                //return the letter here
                returnString = [@"D+" mutableCopy];
                //return @"D+";
            } else if (([[gradeScale valueForKey:@"D+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D"] floatValue])) {
                //return the letter here
                returnString = [@"D" mutableCopy];
                //return @"D";
            } else if (([[gradeScale valueForKey:@"D"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D-"] floatValue])) {
                //return the letter here
                returnString = [@"D-" mutableCopy];
                //return @"D-";
            } else if ([[gradeScale valueForKey:@"D-"] floatValue] > passedGrade) {
                //return the letter here
                returnString = [@"F" mutableCopy];
                //return @"F";
            }
    
            NSLog(@"\nreturnString: %@", returnString);
    i get a blank string at the end of it, and when you debug it and it goes to evaluate the if/else statements, it like “crashes” and blanks out, then comes back and just skipped evaluation of the whole thing. So weird and frustrating. Any help would be great!
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Here's your problem:

    passedGrade is 90.

    gradeScale[@"A-"] is also 90.

    because of the way your code is written, it'll enter none of those cases if passedGrade is exactly equal to any of the values.

    You should use >= instead to compare the values.

    Further, your code is incredibly redundant. I'd use a loop instead that looks more like this:

    Code:
    __block NSString *bestGradeSoFar = @"Ungraded";
        __block float bestGradeSoFarValue = 0;
        [gradeScale enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            float consideringValue = [obj floatValue];
            if (passedGrade >= consideringValue && consideringValue > bestGradeSoFarValue) {
                bestGradeSoFar = key;
                bestGradeSoFarValue = consideringValue;
            }
        }];
    I tested and this works. Just a few small changes I had to make...

    1 - I replaced returnString with bestGradeSoFar... feel free to swap it back. It needs to be a __block variable, though.
    2 - I removed the array from the dictionary. It's bad code smell to be having a dictionary mostly full of one type of key/value pair and then have one key/value pair with a completely different type. I don't think that kind of thing is even allowed in type-checked languages like Java. If you want to stick with that design where you have an array in there, you'll need to add in the a respondsToSelector: inside the loop to prevent it from crashing.
     
  3. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #3
    Nice one, ArtOfWarfare, great example of enumerateKeysAndObjectsUsingBlock usage :)
     

Share This Page