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:
class Foo {
@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
If you want to call a static property/function of Foo:
(Foo.self as GetLatestImage.Type).someStaticProperty
The result is you have Swift code that uses deprecated API without any deprecation warnings.
What about global functions? We can also work around those. Imagine we have a module X
containing the function foo
which is declared @available(iOS, deprecated: 13.0) func foo() -> Int
.
Here, we can work around with a protocol and an enum:
protocol Undeprecated {
static func foo() -> Int
}
enum Deprecated: Undeprecated {
@available(iOS, deprecated: 13.0)
static func foo() -> Int { X.foo() }
}
Calling foo() without deprecation warning is now possible using (Deprecated.self as Undeprecated.Type).foo()
.