136

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.

10
  • possible duplicate of Swift alternative for #pragma clang diagnostic
    – zrzka
    Jul 21, 2015 at 19:28
  • 6
    This is not a duplicate. The other question fails to answer this problem. Jul 25, 2015 at 10:09
  • @ClausJørgensen in which way it fails to answer this problem? There's no other way as stated in answers in linked question. Just conditional compilation or new #available macro where developer should use new methods and fallback to the old ones if new ones are not available.
    – zrzka
    Jul 27, 2015 at 19:53
  • @robertvojta No, as the answers does, in fact, not state that there's no other ways to silence a warning. Jul 27, 2015 at 19:54
  • 3
    This isn't a dupe. What about a situation where you're given a warning for missing an initialiser?
    – NSTJ
    Sep 30, 2015 at 1:35

5 Answers 5

234

As of 2022, Xcode 14.x, Swift 5.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 2023!

8
  • 34
    Damn, that's a bummer. It gets outta hand sometimes. It's annoying to say the least.
    – Isuru
    Jan 7, 2016 at 16:44
  • 4
    I want to down vote this answer a million times, but it does answer the question pretty well so +1 :-)
    – deadbeef
    Jan 29, 2016 at 18:00
  • 4
    @Isuru At that point I'd get irritated enough to just rebuild the whole thing. Guess the warnings worked
    – Allison
    Apr 24, 2016 at 1:02
  • 2
    @Isuru Most of those should be fixed, not ignored.
    – kevin
    Aug 2, 2018 at 7:36
  • 3
    So frustrating! Thanks for keeping this answer updated. Mar 11, 2019 at 20:08
86

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().

12
  • 3
    So clever. Kind of evil? :) But so good. Great for a use case like suppressing warnings over ongoing use of some aspects of the AddressBook framework that have been deprecated, but have a replacement doesn't actually provide all of the required functionality yet. Thanks. Sep 16, 2017 at 23:19
  • 6
    if this works, i will send you a six pack of your favorite drink. you have an outstanding mind sir, thank you.
    – John
    Apr 25, 2018 at 2:38
  • 1
    @John did you send him the six pack? :P This is awesome. Genius. Thanks.
    – Baran
    Sep 12, 2018 at 10:27
  • 1
    Unfortunately with Xcode 14 this workaround does not work anymore. I get a warning like "Conformance of 'DeprecationClass' to 'DeprecationProtocol' is deprecated"
    – thetrutz
    Oct 12, 2022 at 13:22
  • 1
    @ThomasTempelmann I will adapt the answer accordingly! May 26 at 14:33
43

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.

8
  • 11
    Yes, but you will see the same warning when you call your addressBookStatus()... that you mark as deprecated. Oct 18, 2015 at 1:28
  • 4
    Pro tip: if you want to silence it for an entire class just slam this puppy up above your class statement (ex: class ViewController: UIViewController)
    – Allison
    May 30, 2016 at 17:35
  • 2
    @Sirens Then you'll see this warning every time you call this class ☹️ (at least with Xcode 8) Oct 3, 2016 at 3:07
  • Does anyone succeeded in silencing all deprecated warnings with this fix? I was able to decrease their number to just one, but I don't see a way to get rid of the last one. Any suggestions? Oct 4, 2016 at 8:22
  • 1
    So how do use this to silence the warning “cast from 'CGFloat.NativeType' (aka 'Double') to unrelated type 'Float' always fails” when I'm doing an if CGFloat(0).native is Float { … }? Answer: I don't use this because you didn't answer the question. Mar 27, 2017 at 0:23
9

I was having the issue for a top level function outside of a class or struct:

@available(*, deprecated)
func GetImage(url: URL) -> UIImage? { ... }

I've talked to an engineer at Apple and they told me that you can hide the implementation by a protocol and mark extensions as deprecated. Let's see how it works:

  1. Create a protocol that has a similar signature for the function you want to wrap.
  2. Use the protocol on any class or struct in an extension.
  3. Mark the extension as deprecated.

Everything that's within the extension does not bring up any deprecation warnings.

protocol ImageStoreProtocol {
    func imageFromURL(_ url: URL) -> UIImage?
}

class ImageStore {}

@available(*, deprecated)
extension ImageStore: ImageStoreProtocol {
    func imageFromURL(_ url: URL) -> UIImage? {
        return GetImage(url: url) // Warning does't show up
    }
}
2
  • Upvote because of the official Apple lifehack :) Jun 11, 2021 at 10:06
  • But if you call into the class ImageStore().imageFromURL(...), then you get the deprecation warning again. (Xcode 13.4.1)
    – thetrutz
    Aug 23, 2022 at 14:02
2

While there’s no way to silence deprecation warnings in Swift for now, technically you can do that for a particular symbol by editing the header file.

  • Copy the deprecated symbol name
  • Select File > Open Quickly
  • Paste the symbol and press Enter

    Make sure the Swift icon is disabled in the Open Quickly box

  • Select File > Show in Finder

  • Change file permissions to allow editing if necessary
  • Edit the deprecation macros for the symbol. See surrounding APIs for reference. E.g. replace:

__OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_10_10, __IPHONE_3_0, __IPHONE_8_0)

with

__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0)

Now there’s one less distracting warning you can do nothing about.

I know, it’s dirty. But if there’s no replacement API available in the current SDK, it should be safe. Once a new version of Xcode comes out, the change will get overwritten and you will see the warning again. Then you can test the new SDK and OS to make sure the deprecated API is still available and did not get a replacement.

Please comment if you can come up with any downsides.

1
  • 2
    Upvoting for the resourcefulness, but it would leave a dirty taste in my mouth :P
    – Matt
    Apr 3, 2020 at 5:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.