我有一个国家的枚举,返回isoCodes,dialCodes和名字。在推断isoCodes和dialCodes时,它工作得很好,因为它们是字符串值。但是,当我试图映射名称的LocalizeStringKey,并将它们按字母顺序排列时,键只按顺序设置,而不是翻译,这是一个问题。
如何使用SwiftUI按字母顺序检索设置为LocalizedStringKey的枚举国家名称?
这就是我想要做的。
我试图以正确的顺序映射文件中的国家/地区名称的方式:(只按顺序获取本地化的键)
let countries = Country.allCases.map { $0 }我的国家枚举:
import SwiftUI
// MARK: Country ISO3166 definition
enum Country: String, CaseIterable {
case ad
case ae
case af
case ag
case ai
case al
}我的国家名称设置为变量:
// MARK: Country named from localized CountryString
extension Country {
var name: CountryString {
switch self {
case .ad: return .andorra
case .ae: return .unitedArabEmirates
case .af: return .afghanistan
case .ag: return .antiguaBarbuda
case .ai: return .anguilla
case .al: return .albania
}
}设置localizedStringKey的CountryString枚举:
// MARK: Country string name
enum CountryString: LocalizedStringKey {
case andorra
case unitedArabEmirates
case afghanistan
case antiguaBarbuda
case anguilla
case albania
}我的翻译:
// MARK: - CountryString
"andorra" = "Andorra";
"unitedArabEmirates" = "United Arab Emirates";
"afghanistan" = "Afghanistan";
"antiguaBarbuda" = "Antiga & Barbuda";
"anguilla" = "Anguilla";
"albania" = "Albania";发布于 2020-04-14 17:08:58
您可以使用字符串插值,例如:
let countries = Country.allCases.map { $0 }
let cname = countries[x].name
let tname = NSLocalizedString("\(cname)", comment: "xxxx")然后按翻译名称的字母顺序对它们进行排序。
https://stackoverflow.com/questions/61194916
复制相似问题