Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics Introduction to Operators and Conditionals Review Switches and Enums

Armando Torrez
Armando Torrez
3,238 Points

Compiler Issues

In this lesson, I tried running the code regarding popcorn sizes and their values; however upon running the code, the popcornPrice value gives me the value of 0, initially. When I run through the code line by line using Step-in tool, the value changes to the correct one. Please, help me understand why.

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there! Please add your code to the question, it's impossible to go through this otherwise. Thanks!

Armando Torrez
Armando Torrez
3,238 Points
enum popcornSizes {
    kidsPopcorn   = 1,
    smallPopcorn  = 2,
    mediumPopcorn = 3,
    largePopcorn  = 4,
    tubPopcorn    = 5,
};

float popcornPrice;
int popcornSize = tubPopcorn;

switch (popcornSize) {
        case kidsPopcorn:
            popcornPrice = 1.5;
            break;
        case smallPopcorn:
            popcornPrice = 3;
            break;
        case mediumPopcorn:
            popcornPrice = 4.25;
            break;
        case largePopcorn:
            popcornPrice = 5.25;
            break;
        case tubPopcorn:
            popcornPrice = 6;
            break;
        default: NSLog(@"No valid size entered");
            break;
}

    return 0;
}

popcornPrice = 0 popcornSize = 0

When I use the Step-in tool to go over the lines of code, popcornPrice = 6 popcornSize = 5

As it should. But only after I have gone through each functional line of code.

4 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Oh, it seems you have accidentally set a breakpoint. this will stop the app from executing whenever it reaches this point. This is for debugging porposes. Is there a blue rectangle on the line number of this line? If so, you can either drag and drop it out of the editor, remove it in the breakpoint manager or disable breakpoints as a whole.

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

I am not sure if I understand your question correctly, but this is expected behavior. This code is executed in a serial manner. Only after the switch statement is finished, your variables are set correctly.

float popcornPrice;
int popcornSize = tubPopcorn;

At this point popcornPrice is 0, the default value when creating a float without assigning a value. popcornSize, however, is 5 at this point, not zero.

Now the switch statement is executed, and it goes through every single case statement, unless it matches this case. In case of tubPopcorn, it is the last case that matches, so the code in the body of this case is executed. Only now the popcornPrice is assigned a float value of 6.

Not sure what return 0 and the extra } are, I consider this a copy/paste artefact, otherwise it would not compile.

Hope this helps :)

Armando Torrez
Armando Torrez
3,238 Points

The portions of the code do not run until I review them step by step. I'm noticing that there is a green banner over: int popcornSize = kidsPopcorn; saying Thread 1 Breakpoint 2.1

Perhaps that has to do with it?

Armando Torrez
Armando Torrez
3,238 Points

Correction:

int popcornSize = tubPopcorn;

Armando Torrez
Armando Torrez
3,238 Points

Oh, yeah. I removed the first breakpoint and now it's working. Thank you!