Enums 如何按字母顺序排列LocalizedStringKey值?
我列举了一些国家,它们返回等位码、拨号码和名称。当推断等位码和拨号码时,它工作得很好,因为它们是字符串值。但是,当我试图映射名称的LocalizeStringKey并按字母顺序排列时,这些键只按顺序设置,而不按翻译,这是一个问题 如何使用SwiftUI按字母顺序检索设置为LocalizedStringKey的枚举国家的名称 这就是我想做的 我试图以正确的顺序映射文件中的国家名称的方式: (仅按顺序获取本地化密钥) 我的国家列举:Enums 如何按字母顺序排列LocalizedStringKey值?,enums,localization,swiftui,Enums,Localization,Swiftui,我列举了一些国家,它们返回等位码、拨号码和名称。当推断等位码和拨号码时,它工作得很好,因为它们是字符串值。但是,当我试图映射名称的LocalizeStringKey并按字母顺序排列时,这些键只按顺序设置,而不按翻译,这是一个问题 如何使用SwiftUI按字母顺序检索设置为LocalizedStringKey的枚举国家的名称 这就是我想做的 我试图以正确的顺序映射文件中的国家名称的方式: (仅按顺序获取本地化密钥) 我的国家列举: import SwiftUI // MARK: Country
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的My 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";
可以使用字符串插值,例如:
let countries = Country.allCases.map { $0 }
let cname = countries[x].name
let tname = NSLocalizedString("\(cname)", comment: "xxxx")
然后按翻译名称的字母顺序对它们进行排序。您可以使用字符串插值,例如:
let countries = Country.allCases.map { $0 }
let cname = countries[x].name
let tname = NSLocalizedString("\(cname)", comment: "xxxx")
然后根据翻译后的名字按字母顺序排列