关于iphone:如何在switch语句中修复NSString Expected表达式错误?

How to fix an NSString Expected expression error in a switch statement?

本问题已经有最佳答案,请猛点这里访问。

我在下面nsstring中代码的第一行的switch语句中得到一个"expected expression"错误:NSString *emailTitle = @"some text";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
break;
    case 4:
        // mail
        // Email Subject
        NSString *emailTitle = @"some text";
        // Email Content
        NSString *messageBody = @"http://www.example.com/";
        // To address
        NSArray *toRecipents = [NSArray arrayWithObject:@""];

        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc setSubject:emailTitle];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:toRecipents];

        // Present mail view controller on screen
        [self presentViewController:mc animated:YES completion:NULL];

        break;
    case 5:

如果没有这段电子邮件代码,switch语句就可以正常工作。

谢谢你的帮助


不能在case语句中声明变量,因为作用域不明确…

改为下式,范围由括号指定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 case 4:
        {
            // mail
            // Email Subject
            NSString *emailTitle = @"some text";
            // Email Content
            NSString *messageBody = @"http://www.example.com/";
           // To address
            NSArray *toRecipents = [NSArray arrayWithObject:@""];

            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate = self;
            [mc setSubject:emailTitle];
            [mc setMessageBody:messageBody isHTML:NO];
            [mc setToRecipients:toRecipents];

            // Present mail view controller on screen
            [self presentViewController:mc animated:YES completion:NULL];
        }
        break;
    case 5: