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!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

3 Answers3

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)

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • I don't see a SHKShareTypeUndefined in my class where this warning is. Also are you saying that these switch statements are doing nothing? – SimplyKiwi Aug 09 '11 at 12:34
  • 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. – Vladimir Aug 09 '11 at 12:39
  • I had exactly the same problem yesterday and fixed it that way. But finally I just set "Check switch statements" compiler flag to NO – Vladimir Aug 09 '11 at 12:40
  • Thanks for the info, is my new code above what you did to make the warning go away? It went away from me. – SimplyKiwi Aug 09 '11 at 12:43
  • @iBrad Apps, yes, that's exactly what I did – Vladimir Aug 09 '11 at 12:44
  • Setting "check switch statements" to NO feels cleaner to me. Thanks for the tip! – Michael Luton Apr 10 '13 at 04:48
  • @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 everywhere – Vladimir Apr 10 '13 at 08:24
17

You can also use a default case:

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

        // ...

        default:
           NSLog(@"Unexpected case - will do nothing here");
           break;
} 
Guillaume
  • 21,685
  • 6
  • 63
  • 95
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
Nate Chandler
  • 4,533
  • 1
  • 23
  • 32