How to fix warning for Locale.canonicalIdentifier(from:)

Viewed 21

I'm compiling an app for target iOS 14+

When using Locale.canonicalLocaleIdentifier(from:) the compiler shows a warning:

'canonicalLocaleIdentifier(from:)' is deprecated: renamed to 'identifier(_:from:)'`

But documentation for this API states that it is only deprecated in iOS 16.

Furthermore Locale.identifier(_:from:) is only available for iOS 16.

So how could I silence this warning?

1 Answers

This one can simply be fixed by using if #available

if #available(iOS 16, *) {
    return Locale.identifier(.icu, from: <the ID>)
} else {
    return NSLocale.canonicalLocaleIdentifier(from: <the ID>)
}
Related