views:

40

answers:

4

Simple problem... I had some comments on my code and deleted them and got an error. After some hours I arrived at the source.

This code works:

switch (indexPath.row) {
    case 0:
        NSLog(@"case 0");
        break;
    case 1: // Clients
        NSLog(@"case 1");

        ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];//]WithNibName:@"BrowseViewController" bundle:nil];
        viewListTableController.managedObjectContext = self.managedObjectContext;
        [self.navigationController pushViewController:viewListTableController animated:YES];
        //[self.navigationController setNavigationBarHidden:NO];
        [viewListTableController release];

        break;
}

This next one, (by just removing the NSLog(@"case 1"); ) does NOT work:

switch (indexPath.row) {
    case 0:
        NSLog(@"case 0");
        break;
    case 1: // Clients


        ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];//]WithNibName:@"BrowseViewController" bundle:nil];
        viewListTableController.managedObjectContext = self.managedObjectContext;
        [self.navigationController pushViewController:viewListTableController animated:YES];
        //[self.navigationController setNavigationBarHidden:NO];
        [viewListTableController release];

        break;
}

As you can see, only the NSLog line is gone. And the compiler is giving me 2 errors:

RootViewController.m:212: error: expected expression before 'ViewClientListTableController'

RootViewController.m:213: error: 'viewListTableController' undeclared (first use in this function)

Of course, one answer is to leave the NSLog line, but really... why is this error happening?

+2  A: 

switch statements often have trouble with variables being declared in their case labels. I bet

{
        ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];//]WithNibName:@"BrowseViewController" bundle:nil];
        viewListTableController.managedObjectContext = self.managedObjectContext;
        [self.navigationController pushViewController:viewListTableController animated:YES];
        //[self.navigationController setNavigationBarHidden:NO];
        [viewListTableController release];
}

works in case 1: - the NSLog macro probably has an expansion that has a similar effect.

Adam Eberbach
That worked. Thanks
elcool
Great! don't forget to click the check mark if this is the answer you want to accept.
Adam Eberbach
A: 

When using switch-case statements, i also faced that problem. If you write a semicolon before "ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];" sentence like ";ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];", you will probably see there is no error anymore. I don't know the reason exactly, but it corrects the problem.

crazywood
+1  A: 

I have this trouble all the time. I guess that we cannot declare a new varible in the first line inside the case label unless we have a bracket {}

case 1: // Clients
{

        ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];//]WithNibName:@"BrowseViewController" bundle:nil];
        viewListTableController.managedObjectContext = self.managedObjectContext;
        [self.navigationController pushViewController:viewListTableController animated:YES];
        //[self.navigationController setNavigationBarHidden:NO];
        [viewListTableController release];

        break;
}
vodkhang
A: 

Use curly braces for each of your cases. This will fix it.

case 0:
{
    NSLog(@"case 0");
    break;
}
case 1: // Clients
{
    ViewClientListTableController *viewListTableController = [[ViewClientListTableController alloc] init];//]WithNibName:@"BrowseViewController" bundle:nil];
    viewListTableController.managedObjectContext = self.managedObjectContext;
    [self.navigationController pushViewController:viewListTableController animated:YES];
    //[self.navigationController setNavigationBarHidden:NO];
    [viewListTableController release];

    break;
}
muffix