elcool
elcool

Reputation: 6131

Weird Compilation Error on Xcode

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?

Upvotes: 1

Views: 317

Answers (3)

vodkhang
vodkhang

Reputation: 18741

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;
}

Upvotes: 1

crazywood
crazywood

Reputation: 1455

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.

Upvotes: 0

user189804
user189804

Reputation:

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.

Upvotes: 3

Related Questions