dimanche 28 juin 2015

Xcode "switch/case" statement - warning about break;

What constantly happens to me is this, when I'm using Xcode with Objective-C:

I'll be writing a switch/case statement. Typically, to make sure statements are separated, the programmer must make sure there are break;s between cases.

I often forget to put the break; statement in, so instead of the device executing only the the desired case, the device executes the case and then the case after it. This happens on both a physical device (iPhone 6) and on every simulated device on iOS Simulator.

Here's what the syntax of the failed statement looks like, with someInt being a number that is either 0 or 1:

switch (someInt)
{
    case 0:
    {
         // some statements to execute if "someInt" is 0
    }
    case 1:
    {
         // some statements to execute if "someInt" is 1
         break;
    }
}

Note how, in case 0:, there is no break; statement. This causes case 0: to execute and then case 1 to execute, instead of just case 0 to execute.

Is there a way I can put a flag in Xcode so it warns me if I forget a break; statement?

I know many programmers have probably been confused for days, weeks on end, because of this problem.

Aucun commentaire:

Enregistrer un commentaire