Imagine I mark the following method deprecated in Swift:
@available(*, deprecated=1.0)
func myFunc() {
// ...
}
And I treat warnings as errors in Swift by setting OTHER_SWIFT_FLAGS="-warnings-as-errors"
.
How do I make it show these deprecation notices as warnings, while still treating the rest of the warnings as errors?
It seems like GCC had a pretty good solution to this problem:
-Werror // treat all warnings as errors
-Wno-error=<warning> // don't treat <warning> as error (e.g. -Wno-error=switch)
-Werror=<warning> // treat <warning> as error
So if this was Objective-C, I could simply use -Werror -Wno-error=deprecated-declarations
and get exactly what I want.
What is the equivalent for Swift?
I tried adding -Wno-error=deprecated-declarations
to the OTHER_SWIFT_FLAGS
, but it seems like it's not meant for Swift, so it doesn't work.
In Swift, there’s a built-in error handling mechanism that makes your life easy as an iOS dev. In Swift, a function can throw errors. This means an error propagates from a function back to its caller and the caller handles it. The basic way to handle errors is with do-try-catch blocks:
You use WarningsAsErrors to configure a set of warnings as errors. Use WarningsNotAsErrors to configure a set of warnings that should not be errors when you've set all warnings as errors. The DisabledWarnings option lets you suppress the compiler from displaying one or more warnings.
Optionally, if you want only a few specific warnings to be treated as errors, you may specify a comma-separated list of warning numbers to treat as errors. The set of all nullability warnings can be specified with the Nullable shorthand. Use WarningLevel to specify the level of warnings that you want the compiler to display.
The WarningLevel option specifies the warning level for the compiler to display. The element value is the warning level you want displayed for the compilation: Lower numbers show only high severity warnings. Higher numbers show more warnings. The value must be zero or a positive integer:
This isn't possible. As of Swift 4, the Swift compiler doesn't have switches to either enable/disable particular warnings or promote particular warnings to errors.
The Swift core developers expressed their reluctance to add a litany of compiler flags on several occasions on the swift-evolution mailing list. The rationale is that they want to keep a single "dialect" of Swift so that every developer works with the same language features etc.
Whether this should extend to something like particular warning flags is of course debatable, but that's the current official stance. It's definitely possible that these rules will be somewhat loosened in the future, but I wouldn't bet on it.
EDIT: As of Swift 4.2, Swift added a #warning
syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With