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

时间:2013-05-21 17:46:26

标签: iphone ios objective-c nsstring

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

感谢您的帮助

1 个答案:

答案 0 :(得分:17)

你不能在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:
相关问题