skip to Main Content

We are facing the following issue with the following API Locale.isoRegionCodes which has been deprecated from iOS >= 16. The new API is the following Locale.Region.isoRegions but when doing a mapping like the following the region.identifier returns the code 001.

if #available(iOS 16, *) {
            
           let CountryCodeWithNewApi = Locale.Region.isoRegions.map { region in
                CustomCountry(value: "", key: region.identifier)
            }.sorted(by: { $0.value < $1.value })
            
        } else {
            // Fallback on earlier versions
        }

How do we get the old country codes of 2 letters by deriving it from this code? is it possible or we are required to do a manual mapping of the ISO-3166-alpha 2 codes to be able to display all the country codes to the user?

2

Answers


  1. The expression:

    Locale.Region.isoRegions.filter { $0.subRegions.isEmpty }.map { $0.identifier }
    

    gives the same list as the deprecated:

    Locale.isRegionCodes
    

    The following code:

    let oldCodes = Locale.isoRegionCodes.sorted()
    let newCodes = Locale.Region.isoRegions.filter { $0.subRegions.isEmpty }.map { $0.identifier }.sorted()
    print(oldCodes == newCodes)
    

    prints:

    true

    when run on a macOS Swift Playground in Xcode 15.2 under macOS 14.3.1. It should also be true under iOS (but I haven’t confirmed).

    Your code then becomes something like the following:

    if #available(iOS 16, *) {
        let countries = Locale.Region.isoRegions.compactMap {
            $0.subRegions.isEmpty ? CustomCountry(value: "", key: $0.identifier) : nil
        }.sorted(by: { $0.value < $1.value })
    } else {
        let countries = Locale.isoRegionCodes.map { CustomCountry(value: "", key: $0) }
    }
    
    Login or Signup to reply.
  2. When run on a macOS Swift Playground in Xcode 15.2 under macOS 14.3.1. It should also be true under iOS (but I haven’t confirmed).

    Your code then becomes something

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search