Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Swift compiler warning will never be executed

Tags:

xcode

swift

I have code in this vein:

enum enumThingType {
    case apples
    case oranges
    case starfruit
}

func myFunc( enumThing: enumThingType ) -> String {
    switch enumThing {
    case .apples:
        return "Hey, apples"
    case .oranges:
        return "Hey, oranges"        
    default:
        return ""
    }
}

I'm very slowly filling in the code for all of the cases, and I have need of the code to be functional while I code for starfruit and its ilk.

All is good, but the compiler issues the warning will never be executed for return "". It happily generates an executable, but I am compulsive and would like warnings and errors to go away.

If I delete the line return "", then I get the very real and compiler arresting Missing return in a function expected to return 'String'.

Likewise, if I just delete default:..., I get the equally arresting error Switch must be exhaustive, consider adding a default clause, which is fully understandable, but I am creative in my efforts to thwart warnings and errors.

So given this construct, can I eliminate warnings and errors while still coding for existing cases for myFunc?

EDIT

As @appzYourLife pointed out, I omitted precompiler directives in my sample code that were creating the error. This code, with the precompiler directives, throws a warning:

enum enumThingType {
    case apples
    case oranges
    case starfruit
}

func myFunc( enumThing: enumThingType ) -> String {
    switch enumThing {
    case .apples:
        return "Hey, apples"
    case .oranges:
        return "Hey, oranges"        
    default:
    #if DEBUG
        print( "Ouch!" )
    #else
        fatalError()
    #endif
        return ""
    }
}

Because if the product is not created with DEBUG specified, then return can never happen--the FatalError() prevents that.

This code, however, does not throw a warning for all precompiler specified cases:

func myFunc( enumThing: enumThingType ) -> String {
    switch enumThing {
    case .apples:
        return "Hey, apples"
    case .oranges:
        return "Hey, oranges"        
    default:
    #if DEBUG
        print( "Ouch!" )
        return ""
    #else
        fatalError()
    #endif
    }
}

I just wanted to summarize the actual code fix for anyone who might gander across this, as the lack of a properly specified DEBUG flag fixed it for a DEBUG condition, but not for a non DEBUG condition. Thanks again for everyone who helped on this!

like image 310
Dribbler Avatar asked Feb 20 '16 23:02

Dribbler


1 Answers

I follow the chat you had with @matt, so it looks like the code you posted in your question was NOT the real code ;)

The real code is more something like this

func myFunc( enumThing: enumThingType ) -> String {
    switch enumThing {
    case .apples:
        return "Hey, apples"
    case .oranges:
        return "Hey, oranges"
    default:
        #if DEBUG
        print("...")
        #else
        fatalError()
        #endif
        return ""
    }
}

Now I am getting your warning and it is totally correct.

In fact since DEBUG is NOT defined, the #else block will be executed.

So the fatalError() will be executed.

And since you are putting a return "" immediately after a fatalError()... well the return of course will never be executed.

The compiler is right.

Update: defining the DEBUG complier flag

  1. Open the project root
  2. Select your target
  3. Look for the Swift Compiler - Custom Flags section
  4. Add to debug or release this entry: -D DEBUG

enter image description here

like image 57
Luca Angeletti Avatar answered Nov 15 '22 07:11

Luca Angeletti