Declare method unavailable since a specific version of iOS in swift 5

Viewed 733

I wish to achieve something like that:

@unavailable(iOS 11.0, *)
func oldWay() { 
    ...
}

@available(iOS 11.0, *)
func newWay() { 
    ...
}

I've tried things like @available(iOS 11.0, unavailable, *) but it does not compile.

My problem is that I'm conforming to an Objective-C protocol with optional methods. Some of them are only available since iOS 11, my app is available since iOS 10 and I don't want to have both methods implemented for a given platform.

For instance if I do this:

func oldWay() { 
    ...
}

@available(iOS 11.0, *)
func newWay() { 
    ...
}

Both methods are implemented on all platforms since iOS 11... That's not what I'm looking for.

So if anyone has an idea...

1 Answers

You can use the deprecated: argument (and potentially message: as well), because that seems to be what's happening here:

@available(iOS, deprecated: 11.0, message: "Please use 'newWay'")
func oldWay() {

}

@available(iOS 11.0, *)
func newWay() {

}

This will produce a warning if you try to use the old way. If you want an error instead, replace deprecated with obsoleted.

Related