Objective-C格式化样式会导致切换大小写错误 [英] Objective-c formatting style causes an error in a switch-case

查看:140
本文介绍了Objective-C格式化样式会导致切换大小写错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   - (void)在我的switch语句中出现了一些多行的Objective-C代码)mailComposeController:(MFMailComposeViewController *)控制器
didFinishWithResult:(MFMailComposeResult)结果
错误:(NSError *)错误
{
//通知用户有关与接口$ b相关的错误$ b switch(result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultFailed:
// NSLog(@Mail Failed);
UIAlertView * alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@Error,@Error)
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@OK,@OK)
otherButtonTitles:nil];
[alert show];
[警戒释放];
break;
默认值:
break;




$ p
$ b $ p $如果我取消注释 NSLog ,它工作正常。什么导致这个错误?有没有什么办法可以使用这种格式?

解决方案

你不应该在开关 case ,除非您引入一个作用域。

  case MFMailComposeResultFailed:{//<  -  
UIAlertView * alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@Error,@Error)
message:错误localizedDescription]
委托:无
cancelButtonTitle:NSLocalizedString(@OK,@OK)
otherButtonTitles:nil];
[alert show];
[警戒释放];
break;
} //< -

实际错误是因为在C标准  6.8.1),一个标签只能跟一个语句( NSLog(@Mail Failed)),而不是一个声明( UIAlertView * alert = ... )。


I'm getting an error in my switch statement with some multi-line Objective-c code:

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error 
{   
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultFailed:
//              NSLog(@"Mail Failed");
            UIAlertView *alert = [[UIAlertView alloc] 
                                initWithTitle:NSLocalizedString(@"Error", @"Error")
                                message:[error localizedDescription]
                                delegate:nil
                                cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                otherButtonTitles:nil];
            [alert show];
            [alert release];
            break;
        default:
            break;
    }
}

If I uncomment the line with the NSLog, it works fine. What's causing this error? Is there any way to use this kind of formatting?

解决方案

You should not declare a variable in a switch case unless you introduce a scope.

    case MFMailComposeResultFailed: {  // <--
        UIAlertView *alert = [[UIAlertView alloc] 
                            initWithTitle:NSLocalizedString(@"Error", @"Error")
                            message:[error localizedDescription]
                            delegate:nil
                            cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                            otherButtonTitles:nil];
        [alert show];
        [alert release];
        break;
    } // <--

The actual error is because in the C standard (§6.8.1), a label can only be followed by a statement (NSLog(@"Mail Failed")), not a declaration (UIAlertView* alert = ...).

这篇关于Objective-C格式化样式会导致切换大小写错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆