【问题标题】:How to fix an NSString Expected expression error in a switch statement? [duplicate]如何修复 switch 语句中的 NSString 预期表达式错误? [复制]
【发布时间】:2013-05-16 14:13:24
【问题描述】:

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

感谢您的帮助

【问题讨论】:

    标签: iphone ios objective-c nsstring


    【解决方案1】:

    你不能在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:
    

    【讨论】:

    • 为什么范围不明确?为什么范围不是它只存在于该开关内?
    • @TJOlsen 也许这不是他描述它的最佳方式,但由于它通过跳转和标签实现的方式,所有代码都溢出到包含函数的正常平面上下文中。 .
    猜你喜欢
    • 2011-07-07
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多