2012-03-30 5 views
6

다음은 전자 메일을 보낼 수 없을 때 오류 메시지를 표시하는 스위치/사례 문입니다. 대부분의 경우, 모든 것이 잘 보인다,하지만 난 스위치 문에 UIAlertView를 배치 할 때 나는 엑스 코드에서 오류가 얻을 : 나는 내부 코드를 배치 할 때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- arc-what-does-switch-case-in-protected-scop) – 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

문제는 스위치의 경우 내부 변수를 선언한다. 컴파일러는 코드의 일부만 실행될 때 범위를 파악하려고 애를 씁니다. '실패'사례의 내용을 대괄호로 묶으면 범위가 제한되므로 확인해야합니다. [여기에 입력 링크 설명]에서