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.
Edit: I looks like my note was not clear enough: 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.
As of 2018, Xcode 10.0, the consensus is that there is no way to achieve that.
I'll update/edit this answer if Apple add the feature.
Put it in your wish list for WWDC 2019 !
Actually, you can suppress these warnings by using @available
in the enclosing logical structure (i.e. function/type).
For example, say you have some code which uses the AddressBook framework, but you're building against iOS 9.
@available(iOS, deprecated: 9.0)
func addressBookStatus() -> ABAuthorizationStatus {
return ABAddressBookGetAuthorizationStatus()
}
As of Xcode 7.0.1 this will prevent the inline warnings from being displayed.
There is no general construct to silence deprecation warnings in Swift, but there is a workaround that can be applied in many cases.
Let's say you have a method getLatestImage()
on class Foo
which uses deprecated methods/classes.
Use @available
as Daniel Thorpe described to silence all the warnings inside the method:
@available(iOS, deprecated: 9.0)
func getLatestImage() -> UIImage? {
...
}
Now you would like to call the method getLatestImage()
without having a deprecation warning. You can achieve that by first defining a protocol and an extension:
private protocol GetLatestImage {
func getLatestImage() -> UIImage?
}
extension Foo: GetLatestImage {}
And then call the method without a deprecation warning (if foo
is an instance of Foo
):
(foo as GetLatestImage).getLatestImage() // no deprecation warning
The result is you have Swift code that uses deprecated API without any deprecation warnings.
来源:https://stackoverflow.com/questions/31540446/how-to-silence-a-warning-in-swift