iOS从17.1.2更新到17.4.1后,TableView中部分国家的显示发生了变化,例如台湾变成了Root(Taiwn),澳门也变成了Root(Makao),香港和中国也一样。代码一年多来没有更改且正常工作。
private func createCountriesByLang(lang: Langs) -> [CountryModel] {
var result = [CountryModel]()
for code in NSLocale.isoCountryCodes {
let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
let name = NSLocale(localeIdentifier: lang.rawValue).displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
result.append(CountryModel(name: name, code: code))
}
switch lang {
case Langs.en:
result.append(CountryModel(name: "Zaire", code: "ZR"))
result.append(CountryModel(name: "British Indian Ocean Territory", code: "IO"))
result.append(CountryModel(name: "Netherlands Antilles", code: "AN"))
case Langs.ru:
result.append(CountryModel(name: "Заир", code: "ZR"))
result.append(CountryModel(name: "Британская Территория в Индийском Океане", code: "IO"))
result.append(CountryModel(name: "Нидерландские Антильские о-ва", code: "AN"))
case Langs.zh:
result.append(CountryModel(name: "扎伊尔", code: "ZR"))
result.append(CountryModel(name: "英属印度洋领地", code: "IO"))
result.append(CountryModel(name: "荷属安的列斯", code: "AN"))
}
return result
}
尝试使用而不是displayName
let name = NSLocale(localeIdentifier: lang.rawValue).localizedString(forCountryCode: code)