2013-05-21 38 views
1

我下面的代碼在這個NSString的第一行收到「預期的表達」錯誤的switch語句:NSString *emailTitle = @"some text"; 如何修復switch語句中的NSString預期表達式錯誤?

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語句工作正常。

感謝您的幫助

回答

15

你不能在聲明的情況下,語句的變量,因爲範圍不明確......

變化以下,其中由括號{指定範圍}

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: