ios there is a way to combined switch cases? - objective-c

I was wondering if there is a way to combined switch cases for example:
switch (value)
{
case 0,1,2:
nslog (#"0,1,2 cases");
break
case 3:
nslog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
I'll really appreciate your help

You mean, something like this?
switch (value)
{
case 0:
case 1:
case 2:
NSLog (#"0,1,2 cases");
break;
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
You know, the switch case structure will execute each line inside the braces starting from the corresponding case line, until it reach the last one or a break. So, if you don't include a break after a case, it will go on executing the next case also.

Alternatively, you can do this...
case 0:
case 1:
case 2:
NSLog();
break;
case 3:
NSLog()
break;
default:
NSLog();
break;

It is also possible to use ranges (a little bit less code). The following example illustrates it:
switch (value)
{
case 0 ... 2:
NSLog (#"0,1,2 cases");
break
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}

Related

Objective-C A switch statement inside another switch statement

So I have created a switch statement inside another switch statement for certain sections and rows, quite hard to explain so have a look at the actual code. Anyways I was wondering if such patter should be avoided or if it does not really matter, I am new to stackoverflow as you may have already noticed so I am not entirely sure this question has been asked before.
switch(indexPath.section) {
case 0: {
switch ([indexPath row]) {
case 0: {
[self.navigationController pushViewController:introductionViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
case 1: {
[self.navigationController pushViewController:matterViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
break;
}
case 1: {
break;
}
}
}
A switch statement inside of a switch statement is perfectly legal. There's nothing inherently wrong with it, though it may be difficult to follow if there are many cases in either statement. One solution is to make the inner switch statement into a separate function or method. For example, if you have this:
switch (a)
{
case foo:
switch (b)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
break;
case foo2:
...whatever
}
You could do it like this:
void handleFoo (int x)
{
switch (x)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
}
...
main ()
{
...
switch (a)
{
case foo:
handleFoo(b);
break;
case foo2:
...whatever;
break;
}
}

Switch statement syntax for same action through different cases

Two constants (1+2) share the same case statement. I donĀ“t want to double the code.
What is the right syntax to do this?
switch (expression) {
case 0:
[self taskA];
break;
case 1:
[self taskB];
break;
case 2:
[self taskB]
break;
default:
break;
}
Use :
switch (expression) {
case 0:
[self taskA];
break;
case 1:
case 2:
[self taskB];
break;
default:
break;
}
Edit 1:
In switch we say a term called fall-through. Whenever control reaches to a label say case 0: it falls till break is found. On break control is sent to the closing braces of switch.
If break is not encountered it goes to next case as in case then case 2. So above case 1 and case 2 shares one break statement.
Multiple case labels can refer to the same statement if break or return are not used at the end of the case.
If you do not use a break statement in case 1, the execution flows into case 2.

Add action to NSRunAlertPanel's button

How can I add action to buttons of NSRunAlertPanel. Here's how I am creating alert panel:
NSRunAlertPanel(#"App Updater",
#"Network Error! ....... ....",
#"Ok",
#"Contact Support",
nil);
I think the easiest thing to do in this case is to check the return value and then take the appropriate action:
NSInteger code = NSRunAlertPanel(...);
switch(code) {
case NSAlertDefaultReturn:
break;
case NSAlertAlternateReturn:
break;
case NSAlertOtherReturn:
break;
}

Section's and cases

A small issue over here i have 4 sections when i click on the first TableCell of a section (got 4 sections) the NSlogs output is:
2012-02-20 20:33:16.870 [8880:c07] 00
Can i do something like;
if switch(indexPath.row == 0) {
case
} else if(indexPath.row == 1) {
case
}
to 'link' each Tablecell to another function?
The problem is when i use the code ;
case 0:
NSLog(#"00");
break;
each first Tablecell of a section gives me the NSlog 00
UIViewController *controller;
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
I think you are looking for something like this:
if (indexPath.section == 0) {
switch(indexPath.row) {
case 0:
NSLog(#"00");
break;
case 1:
NSLog(#"01");
break;
case 2:
NSLog(#"02");
break;
case 3:
NSLog(#"03");
break;
case 4:
NSLog(#"04");
break;
}
} else if (indexPath.section == 1) {
switch(indexPath.row) {
case 0:
NSLog(#"10");
break;
case 1:
NSLog(#"11");
break;
case 2:
NSLog(#"12");
break;
case 3:
NSLog(#"13");
break;
case 4:
NSLog(#"14");
break;
}
}
The if statement separates the execution path on the section, and then switch further separates it on the row. If the number of sections is significant, you could use an outer switch outside of the inner switches.

Odd compiler error when using Obj-C objects in a switch statement

I get a compiler error when using an Objective-C object within a switch statement:
switch (myConstant)
{
case 0:
UIViewController *myController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:myViewController animated:YES];
break;
case 1:
// stuff
break;
default:
break;
}
The error states:
Expected expression before 'UIViewController'
'myViewController' undeclared (first use in this function)
I understand that the second error is a direct result of the first error, but what I don't understand is why I get the 'expected expression' error in the first place...
If I put a ; at the end of the case 0: line, then it will compile, but I shouldn't have to do this, no?
This will also fail to compile, with the same error:
switch (0)
{
case 0:
int a = 0;
break;
default:
break;
}
However, if I declare the int a; outside of the switch block, then a = 0; compiles fine.
I thought I understood switch statements - clearly I don't. Could someone please explain?
Just add {} within the case if you declare a new variable. The case part of a switch statement is not a correct scope to declare variables in C.
case 0:
{
int a = 0;
break;
}
...