开发者

UISharedApplication bug that cannot be determined

-(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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜