swiftpragma

How to silence a warning in Swift?


I have a piece of code which is generating lots of warnings (deprecated API)

Using clang* I could do:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    ...
#pragma clang diagnostic pop

However this does not work in Swift.

How to do it in Swift?

Note: I don't want to disable the warning globally, nor even file wide, but just disable a specific warning in a specific part of my source code.

I do not want conditional compilation (which is the proposed answer of the supposed duplicate). I just want to silence a warning WITHOUT using the new APIs.


Solution

  • As of 2024, Xcode 16.x, Swift 6.x, the consensus is that there is no direct way to achieve that.

    I'll update/edit this answer if Apple adds the feature.

    Put it in your wish list for WWDC 2025!