Expected expression error when creating new variable in switch case block
So I am coding along in my iOS app and I come across an Expected expression error when I am creating a new variable in switch case block. For example, this looks innocent enough…
switch (actionType) { case 0: NSArray *componentArray = [theString componentsSeparatedByString:@","]; // do more stuff here break; // more cases here } |
No matter what I do, the Xcode compiler complains about the NSArray line right below the case 0 statement. What kind of horse hockey is this? The code is so simple that it is laughable.
My investigations led me to discover that there is a C compilation rule that specifies that you cannot have a variable declaration right after a label. And apparently, case statements are compiled into labels. (By exactly what sorcery escapes me, I am sure if you are interested you can dig into the C language definitions and figure it out.)
So I am forced to do this instead:
NSArray *componentArray; switch (actionType) { case 0: componentArray = [theString componentsSeparatedByString:@","]; // do more stuff here break; // more cases here } |
BTW, Happy Birthday to Cheryl Burke, who is by far my favorite pro dancer on Dancing With The Stars. I hope Cheryl returns to the show at some point in the future.