15

我在下面的代码中收到警告枚举值“SHKShareTypeUndefined”未在 switch 中处理。我将相关行和指针加粗:

    + (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 中,我不知道如何修复它。

谢谢!

4

3 回答 3

29

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

case SHKShareTypeUndefined:         
     break;

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

于 2011-08-09T12:24:53.580 回答
17

您还可以使用default案例:

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

        // ...

        default:
           NSLog(@"Unexpected case - will do nothing here");
           break;
} 
于 2011-10-08T12:37:33.960 回答
4

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

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

switch (type) {
    //...
}

#pragma clang diagnostic pop
于 2013-01-08T22:14:13.803 回答