2012-03-30 5 views
6

以下は、電子メールを送信できないときにエラーメッセージを表示するSwitch/Caseステートメントです。ほとんどの部分については、すべてが右のようだが、私はswitch文の中にUIAlertViewを置くとき、私はXcodeでエラーが発生します。Obj-Cのスイッチステートメントの使用

Xcode error

switch (result) { 
    case MFMailComposeResultCancelled: 
     NSLog(@"Result: Mail sending canceled"); 
     break; 
    case MFMailComposeResultFailed: 
     NSLog(@"Result: Mail sending failed"); 
     UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Sending Failed" 
                  message:@"The email could not be sent." 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 

     [message show]; 
     break; 
    default: 
     NSLog(@"Result: Mail not sent"); 
     break; 
} 

私は内部のコードを配置したときに、なぜそれがエラーを生成しませんcase

+0

[ここにリンクの説明を入力します] [1] 見[1]:http://stackoverflow.com/questions/366073/instantiating-new-object-within-switch-block-why- does-it-fail – TompaLompa

+0

いいえ、UIAlertViewはIBActionを必要としません。 – c0d3Junk13

+0

複製可能[ARCを使用するようにプロジェクトを変換するときに、 "スイッチケースが保護された範囲にある"とはどういう意味ですか?](http://stackoverflow.com/questions/7562199/when-converting-a-project-to-use-アークは何ですか?スイッチケースの保護スコープにあります) – ughoavgfhw

答えて

14

括弧に入れて:

case MFMailComposeResultFailed: { 
    NSLog(@"Result: Mail sending failed"); 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Sending Failed" 
                 message:@"The email could not be sent." 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 

    [message show]; 
    break; 
    } 
+0

なぜですか?説明してください。 –

+0

@Phillip Millsの答えを参照するか検索してください。多くの説明があります。 – bandejapaisa

+0

私はそれを得ました.......... –

12

問題はスイッチのケース内部の変数を宣言しています。コンパイラは、コードの一部しか実行されていないときにスコープを把握しようとすることについて怒っています。 '失敗'ケースの内容のまわりに角括弧を入れると、それは範囲を制限するのでOKです。

関連する問題