问题
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:
Without this piece of email code the switch statement works fine.
thanks for any help
回答1:
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:
来源:https://stackoverflow.com/questions/16676313/how-to-fix-an-nsstring-expected-expression-error-in-a-switch-statement