Suppressing deprecated warnings

Is there swift equivalent of:

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

1 Like

There is none in Swift.

Nothing at all? I wouldn't mind setting flags in Xcode's build phases panel.

It's not the Swift way. At least not yet.

See for instance

1 Like

That's interesting, thank you.

I can switch all warnings off via "-suppress-warnings" in "other swift flags", but can't do it on the individual file basis in "build phases" -> "compile sources" -> "compiler flags". (it's not the great UX that I can change it there but then nothing happens).

I can’t comment on the evolution side of this but, on the practical front…

  • If you’re specific concern is testing deprecated code, mark your test method as deprecated. The system will still run it but, because it’s deprecated, it won’t generate deprecation warnings.

  • In other situations I use the protocol workaround described here.

Needless to say, the second point does not make me happy (r. 31131633)-:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

5 Likes

Thank you, the protocol hack removed lots of deprecation warnings from my code. I can't apply it for settable properties though:

private protocol HasName {
    var name: String? { get set }
}

struct Foo: Codable, HasName {
    @available(*, deprecated, renamed: "realName")
    var name: String?
    var realName: String? {
        get {
            // return name // Warning: 'name' is deprecated: renamed to 'realName'
            return (self as HasName).name // WORKS!
        }
        set {
            self.name = newValue // Warning: 'name' is deprecated: renamed to 'realName'
            (self as HasName).name = newValue
            // Error: Cannot assign to immutable expression of type 'String?'
        }
    }
}

or is there another hack?

For those of us who can't see it, is that about an ability to specify compiler flags per file?

is there another hack?

I’ve not looked into that in detail but my general strategy for dealing with such things is to introduce a deprecated function that wraps the facility I need. That ends up being a normal deprecated function, so I can use the protocol hack to call it from a non-deprecated context.

For those of us who can't see it, is that about an ability to specify
compiler flags per file?

It’s more like a general cry for help (-:

If this topic is near and dear to your heart, I encourage you to bring it up again on Swift Evolution (after reading up on previous attempts).

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

the crux of the matter is that self as HasName is an r-value (it is immutable). The following looks ok-ish although it's a bit scary to force-case to a type back from protocol. Will give it a try.

private protocol HasName {
    var name: String? { get set }
}

struct Foo: Codable, HasName {
    @available(*, deprecated, renamed: "realName")
    var name: String?
    var realName: String? {
        get {
            return (self as HasName).name
        }
        set {
            var x = self as HasName
            x.name = newValue
            self = x as! Foo
        }
    }
}
Terms of Service

Privacy Policy

Cookie Policy