如何修复 switch 语句中的 NSString 预期表达式错误? [英] How to fix an NSString Expected expression error in a switch statement?

查看:62
本文介绍了如何修复 switch 语句中的 NSString 预期表达式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting an "expected expression" error in a switch statement on the first line of the code below in this NSString: 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 语句就可以正常工作.

Without this piece of email code the switch statement works fine.

感谢您的帮助

推荐答案

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

you can't declare a variable in a case statement, because the scope is ambiguous...

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

change to the below, where the scope is specified by the brackets {}

 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 语句中的 NSString 预期表达式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆