views:

38

answers:

2
-(void) setupMyLocation {

NSArray *viewControllerArray = [navigationUpdateFromDetail.navigationController viewControllers];
NSUInteger parentViewControllerIndex = [viewControllerArray count] - 2;
NSLog(@"Parent view controller: %@", [viewControllerArray objectAtIndex:parentViewControllerIndex]);

switch(parentViewControllerIndex){
    case 0:

    self.myLocation = navigationUpdateFromDetail.currentLocation;
        break;
    case 1:
        YANAVAppDelegate *currentObject = (YANAVAppDelegate*)[[UIApplication sharedApplication]delegate];

    //  self.myLocation = currentObject.givenLocation;

        break;
    default: break;
}

}       

I cannot seem to figure out why i keep getting an error "expecting expression before YANAVAppdelegate".

I cannot seem to put my finger on why this will not compile. any input is appreciated.. thanks in advance.

+2  A: 

Don't define the variable (currentObject) inside the switch statement.

Felix
thank you, that haunted me for a good 30 mins
Makinitez21
A: 

If you need to define a variable inside a case statement, you can do it using curly brackets (this technique also works in C++, by the way):

-(void) setupMyLocation {

    NSArray *viewControllerArray = [navigationUpdateFromDetail.navigationController viewControllers];
    NSUInteger parentViewControllerIndex = [viewControllerArray count] - 2;
    NSLog(@"Parent view controller: %@", [viewControllerArray objectAtIndex:parentViewControllerIndex]);

    switch(parentViewControllerIndex) {
        case 0:
            self.myLocation = navigationUpdateFromDetail.currentLocation;
            break;

        case 1:
        {
            YANAVAppDelegate *currentObject = (YANAVAppDelegate*)[[UIApplication sharedApplication]delegate];

        //  self.myLocation = currentObject.givenLocation;

            break;
        }

        default: 
            break;
    }
}       

A C++ guru explained me once that this gives your variables a context stack for them to exist, which the switch / case statement does not provide automatically. Remember to delete / release objects if you create any in that context, otherwise it's an easy way to have a memory leak.

I personally always use curly brackets in my case statements, if you ask me ;) you never know if in the future you'll need them, it makes things easier to understand.

Adrian Kosmaczewski
thank you, that helped
Makinitez21