Enumeration value 'SHKShareTypeUndefined' not handled in switch
Asked Answered
K

3

15

I get the warning Enumeration value 'SHKShareTypeUndefined' not handled in switch in the below code. I bolded the relevant line and pointer:

    + (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];
    }

This warning is in ShareKit and I am not sure how to fix it.

Thanks!

Kono answered 9/8, 2011 at 12:23 Comment(0)
M
29

Add dummy case for that enum value:

case SHKShareTypeUndefined:         
     break;

Or set your "Check switch statements" flag to NO in your target settings (warnings section)

Marko answered 9/8, 2011 at 12:24 Comment(7)
I don't see a SHKShareTypeUndefined in my class where this warning is. Also are you saying that these switch statements are doing nothing?Kono
It seems when "Check switch statements" flag is YES it checks if you test for all possible enum values in switch cases - so to avoid that warning you have to add SHKShareTypeUndefined case as well (check what values SHKShareType can have) and that case branch does not have to do anything.Marko
I had exactly the same problem yesterday and fixed it that way. But finally I just set "Check switch statements" compiler flag to NOMarko
Thanks for the info, is my new code above what you did to make the warning go away? It went away from me.Kono
@iBrad Apps, yes, that's exactly what I didMarko
Setting "check switch statements" to NO feels cleaner to me. Thanks for the tip!Gemma
@MichaelLuton, it is easier, but in some cases you may want to enable that warning. e.g. you're adding new values to you enum type and want to make sure it will be handled everywhereMarko
S
17

You can also use a default case:

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

        // ...

        default:
           NSLog(@"Unexpected case - will do nothing here");
           break;
} 
Symon answered 8/10, 2011 at 12:37 Comment(4)
I could but it will technically be the same as the same way above. Up-Voted answer anyway!Kono
Agreed, similar to accepted answer. Just more generic. It's useful when you want to have more than one case not handled explicitly.Symon
yes you can also hide this warning by adding default : break;Sofko
This will handle future new switch cases also. Helpful when using third party libs that are extended from Apple's core framework. e.g. WEPopoverPredecessor
D
4

If you have reason to want neither to add cases for all values of the enum nor to add a default case, and if you're compiling with clang, you can write

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

switch (type) {
    //...
}

#pragma clang diagnostic pop
Druse answered 8/1, 2013 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.