-1
#pragma mark AlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];

    switch (buttonIndex) {
        case 0:
            UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
            NSString * URLString =@"http://www.google.com";
            NSURL *URL = [NSURL URLWithString: URLString];
            NSURLRequest *request=[NSURLRequest requestWithURL:URL];
            [webview loadRequest:request]; // Use of undeclared identifier webview
            [self.view addSubview:webview]; // Use of undeclared identifier webview
            break;
        case 1:
            //if Yes button pressed on logout alert
            [self notificationAction];
            [userdefault removeObjectForKey:@"Login"];
            [userdefault synchronize];
            break;
        default:
            break;
    }
}

This is the code I am using to display a webpage. However in Xcode I get the following error on the 1st line of 1st case:

Expected Expression

Am I missing a header file that I'm suppose to import or am I doing something wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • 3
    Show more of your code above this snippet - the shown code is perfectly valid. You probably have a syntax error somewhere above it. – Undo Nov 25 '13 at 17:59
  • 2
    Good advice: don't name your variables `nsrequest` and `nsurl`, those are easy to confuse with the types. Call them `urlString`, `url` and `request`. –  Nov 25 '13 at 18:01
  • Good Advice. I changed the name of the variables but the code error is being pointed to webview – software is fun Nov 25 '13 at 18:12
  • 1
    It's also a good idea to keep your variable names *camelCase* with lowercase first letter. (eg. `url`, `urlString` etc.) – Filip Radelic Nov 25 '13 at 18:35

1 Answers1

2

When defining a variable on a first line inside a case, you have to wrap code of that case in { }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];

    switch (buttonIndex) {
        case 0:
        {
            UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
            NSString * URLString =@"http://www.google.com";
            NSURL *URL = [NSURL URLWithString: URLString];
            NSURLRequest *request=[NSURLRequest requestWithURL:URL];
            [webview loadRequest:request]; // Use of undeclared identifier webview
            [self.view addSubview:webview]; // Use of undeclared identifier webview
            break;
        }
        case 1:
            //if Yes button pressed on logout alert
            [self notificationAction];
            [userdefault removeObjectForKey:@"Login"];
            [userdefault synchronize];
            break;
        default:
            break;
    }
}

Why? Apparently C has problems with scope when defining a variable inside switch-case. Wrapping the code inside { } creates a new local scope.

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
  • 1
    This is half true. You only need the curly braces if the 1st line after a `case X:` line is a variable declaration. If the 1st line isn't a variable declaration then you can have variable declarations on other lines without any curly braces. – rmaddy Nov 25 '13 at 18:30
  • True, updated my answer to include that. – Filip Radelic Nov 25 '13 at 18:32
  • 1
    To complete @rmaddy's half truth - a *switch statement* in C is a *computed goto* where the body of the switch is a single *block* and control is transferred to one of the *labels* within that block. A block is a sequence of *statements* and *declarations*, and any declarations are *scoped* to their containing block. And, finally :-), only *statements* can be labelled in C, not declarations. Placing the statements in `{` & `}` as in the answer forms a *compound statement* / *block* - as a statement it can be labelled, and as a block it limits the scope of the introduced variables. Isn't C fun! – CRD Nov 25 '13 at 19:10