This question already has answers here:
Switch statement syntax for same action through different cases
(2 answers)
Closed 9 years ago.
I have a switch case where at different case may correspond the same fragment of code.
Is there an elegant alternative, rather than duplicating all the code ?
switch(expression) {
case firstCase:
// do something
break;
case secondCase:
// do exactly the same of first case
break;
case otherCase:
// do anything else
break;
case etceteraCase:
.......
default:
break;
}
I know that as an alternative I could create a method to be called within the case, but this could lead to proliferation of methods hard to maintain.
Yes:
switch(expression) {
case firstCase:
case secondCase:
// do exactly the same of first case
break;
case otherCase:
case yetAnotherCase:
// do anything else
break;
case etceteraCase:
.......
default:
break;
}
Remove break after first case:
switch(expression) {
case firstCase:
case secondCase:
// do the same of first case
break;
case otherCase:
// do anything else
break;
case etceteraCase:
.......
default:
break;
}
switch(expression) {
case firstCase:
case secondCase:
// do exactly the same of first case
break;
case otherCase:
// do anything else
break;
case etceteraCase:
.......
default:
break;
}
excluding break; will produce your expected result.
Related
I'm trying to use a variable as the case match, however I get "Expression is not an integer in Objective-C.
Is it possible to use variable in switches in this manner?
int count = [array count];
switch ([number to check]) {
case 0:
//first statement
break;
case 1 ... (count -1):
//somewhere between 1 and the next to last statement
//Basically the middle
break;
case count:
//Last statement
default:
break;
}
Objective-C (and C) switch only supports a single primitive constant value for each case statement (or a range as pointed out in the answer by TwoStraws). You would be much better off writing your code using if/else:
if ([number to check] == 0) {
} else if ([number to check] >= 1 && [number to check] < count) {
} else if ([number to check] == count) {
} else {
}
Objective-C's switch statement does support ranges of values as you've seen, but doesn't support variable matches I'm afraid.
So, the below code is valid because I've used exact integers:
int numberOfKittens = 12;
NSString *kittenDescription;
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = #"Need more kittens";
break;
case 6 ... 10:
kittenDescription = #"That's a good number of kittens.";
break;
case 11 ... 20:
kittenDescription = #"Are you sure you can handle that many kittens?";
break;
default:
kittenDescription = #"You must really love kittens!";
}
…but trying to put a variable in place of any of those will fail.
If this is something you desperately want, consider using Swift because it has a much more expressive switch matching system. Here's that same code in Swift, now with a variable being used to match a case:
let upperLimit = 20
let numberOfKittens = 19
var kittenDescription = ""
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = "Need more kittens"
case 6 ... 10:
kittenDescription = "That's a good number of kittens."
case 11 ... upperLimit:
kittenDescription = "Are you sure you can handle that many kittens?"
default:
kittenDescription = "You must really love kittens!"
}
Sorry to ask this (I rarely use switch statements) but I am getting an error with this but it seems valid to me (but obviously isn't):
NSInteger section=indexPath.section;
switch(section){
case 0:
Item *mi = self.miArray[indexPath.row]; // <- expected expression
...
return cell;
break;
case 1:
Item *mi = self.miArray[indexPath.row];
break;
}
What am I doing wrong?
You can either put the case in braces (case 0: { Item *mi ... }) or add a ; after the case statement (case 0:;).
Either of that should help but I actually forgot why this is necessary.
Found an explanation here: Weird Switch error in Obj-C
Declaration is not allowed within switch.
Declare Item before entering to switch and do the initialization within the switch.
This question already has answers here:
Create multiple numbered variables based on a int
(2 answers)
Closed 8 years ago.
I'm using
for(int k=i;k<6;k++){
int q=k+1;
switch (q) {
case 1:
textbox1.hidden=YES;
break;
case 2:
textbox2.hidden=YES;
break;
case 3:
textbox3.hidden=YES;
break;
case 4:
textbox4.hidden=YES;
break;
case 5:
textbox5.hidden=YES;
break;
case 6:
textbox6.hidden=YES;
break;
default:
textbox1.hidden=NO;
break;
}
}
I was wondering if there isn't anyway to use make something like this:
[#"textbox%#.hidden] = YES
or something like that..
The second question
I have to do something likes this:
[textbox2 setKeyboardType:UIKeyboardTypeDecimalPad];
But since Im on a for .. I can't put textbox2 I need to put "textbox%#, i" So it can detect on which textbox it is analyzing any idea?
You cannot make the replacement you're asking about. It's probably possible to figure out some alternative using reflection but the resulting code will be much uglier than what you've already got. You could, however, take advantage of your q being 1-6 and use an array of text boxes:
id textboxes[] = {textbox1, textbox2, ... textbox6};
if ((q >= 1) && (q <= 6)) textboxes[q-1].hidden = YES;
else textbox1.hidden=NO;
Is there a way to use switch statement with ranges in Objective C (in XCode), hypothetically something like this:
- (NSString *)evaluate:(NSInteger)sampleSize
{
NSString returnStr;
switch (sampleSize)
{
case sampleSize < 10:
returnStr = #"too small!";
break;
case sampleSize >11 && sampleSize <50:
returnStr = #"appropriate";
break;
case sampleSize >50:
returnStr = #"too big!";
break;
}
return returnStr;
}
There is a GCC extension (which I assume is supported in Clang) that might be suitable for you. It allows you to use ranges in case statements. The full documentation is at http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges - an example case statement from that page is
case 1 ... 5:
which would match (unsurprisingly) 1, 2, 3, 4, or 5.
No, switch statements are for constant values in most languages... The closest you can get is to flow the cases into one another like this:
switch(sampleSize)
{
case 0:
case 1:
case 2:
returnStr = #"too small!";
break;
}
Alternatively, this Question may help...
EDIT: I just thought of another way: you could "#define" that large list of cases in a .h file like this:
#define TOO_LOW case 0: \
case 1: \
case 2: \
case 3:
and then use it in a switch like so:
switch(sampleSize)
{
TOO_LOW
returnStr = #"too small!";
break;
}
Of course, thats not the cleanest solution. What's wrong with 3 "if/else's"?
- (NSString *)evaluate:(NSInteger)sampleSize
{
NSString returnStr;
switch (sampleSize)
{
// for sampleSize between 0 and 10
case 0 ... 10:
returnStr = #"too small!";
break;
// for sampleSize between 11 and 50
case 11 ... 50:
returnStr = #"appropriate";
break;
// for sampleSize above 50
case 50 :
case default:
returnStr = #"too big!";
break;
}
return returnStr;
}
Please Note: This is a solution i worked out but it will not count if sampleSize has h value less than 0.
Simply pass true in your switch statement like below code
- (NSString *)evaluate:(NSInteger)sampleSize {
NSString returnStr;
switch (true)
{
case sampleSize < 10:
returnStr = #"too small!";
break;
case sampleSize >11 && sampleSize <50:
returnStr = #"appropriate";
break;
case sampleSize >50:
returnStr = #"too big!";
break;
}
return returnStr;
}
This will solve your problem
I just Don't understand how to use a boolean operator inside a switch statement
switch (expression) {
case > 20:
statements
break;
case < -20:
statements
break;
}
Edit:
I don't want an If () statement.
You can't. Use if() ... else ....
The nearest thing available to what you want uses a GCC extension and is thus non-standard. You can define ranges in case statements instead of just a value:
switch(foo)
{
case 0 ... 20: // matches when foo is inclusively comprised within 0 and 20
// do cool stuff
break;
}
However, you can't use that to match anything under a certain value. It has to be in a precise range. Switches can only be used to replace the comparison operator against a constant, and can't be used for anything more than that.
switch ((expression) > 20) {
case true:
statements
break;
case false:
default:
statements
break;
}
What.. you want more than 1 boolean in a case? You could do this
int ii = ((expression) > 20) + 2 * ((expression) < -20);
switch (ii) {
case 1:
statements
break;
case 2:
statements
break;
}
This, IMO is pretty bad code, but it is what you asked for...
Just use the if statement, you'll be better off in the long run.