Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#error in Swift (how to mark compile-time error)

What is swift replacement of traditional c-style #error keyword?

I need it to raise compile-time error when pre-defines failed:

#if CONFIG1
    ...
#elseif CONFIG2
    ...
#else
    #error "CONFIG not defined"
#endif
like image 441
brigadir Avatar asked Jul 13 '16 10:07

brigadir


1 Answers

Great news - if you're using Swift 4.2 or newer you can now use #error() and #warning()

For example:

let someBoolean = true

#warning("implement real logic in the variable above") // this creates a yellow compiler warning

#error("do not pass go, do not collect $200") // this creates a red compiler error & prevents code from compiling

Check out the implemented proposal here https://github.com/apple/swift-evolution/blob/master/proposals/0196-diagnostic-directives.md

like image 77
Trev14 Avatar answered Oct 11 '22 14:10

Trev14