在交换机中未处理枚举值'SHKShareTypeUndefined'

时间:2011-08-09 12:23:27

标签: objective-c ios enumeration sharekit

我在下面的代码中的switch中没有处理警告枚举值'SHKShareTypeUndefined'。我加粗了相关的线和指针:

    + (NSArray *)favoriteSharersForType:(SHKShareType)type
{   
    NSArray *favoriteSharers = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%i", SHK_FAVS_PREFIX_KEY, type]];

    // set defaults
    if (favoriteSharers == nil)
    {
        switch (type) 
        {
            case SHKShareTypeURL:
                favoriteSharers = [NSArray arrayWithObjects:@"SHKTwitter",@"SHKFacebook",@"SHKReadItLater",nil];
                break;

            case SHKShareTypeImage:
                favoriteSharers = [NSArray arrayWithObjects:@"SHKMail",@"SHKFacebook",@"SHKCopy",nil];
                break;

            case SHKShareTypeText:
                favoriteSharers = [NSArray arrayWithObjects:@"SHKMail",@"SHKTwitter",@"SHKFacebook", nil];
                break;

            case SHKShareTypeFile:
                favoriteSharers = [NSArray arrayWithObjects:@"SHKMail", nil];
                break;

            case SHKShareTypeUndefined:         
                break;
        }

        // Save defaults to prefs
        [self setFavorites:favoriteSharers forType:type];
    }

此警告在ShareKit中,我不知道如何修复它。

谢谢!

3 个答案:

答案 0 :(得分:29)

为该枚举值添加虚拟案例:

case SHKShareTypeUndefined:         
     break;

或者在目标设置(警告部分)中将“检查开关语句”标志设置为NO

答案 1 :(得分:17)

您还可以使用default案例:

switch (type) {
        case SHKShareTypeURL:
            favoriteSharers = ...
            break;

        // ...

        default:
           NSLog(@"Unexpected case - will do nothing here");
           break;
} 

答案 2 :(得分:4)

如果您有理由既不想为枚举的所有值添加案例也不想添加默认案例,并且如果您使用clang进行编译,则可以编写

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch"

switch (type) {
    //...
}

#pragma clang diagnostic pop