2

我在这个 NSString 中下面代码的第一行的 switch 语句中收到“预期表达式”错误:NSString *emailTitle = @"some text";

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 语句可以正常工作。

谢谢你的帮助

4

1 回答 1

18

您不能在 case 语句中声明变量,因为范围不明确...

更改为以下内容,其中范围由方括号 {} 指定

 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:
于 2013-05-21T17:47:55.640 回答