Voglio usare l'enumerazione con stringa localizzata, quindi mi piace, funziona, ma il problema di questa soluzione è: non riesco a ottenere facilmente il valore enum dalla stringa localizzata, devo avere la chiave per farlo:Enum con stringa localizzata in swift
let option = DietWithoutResidueOption(rawValue: "NoDiet")
Se non devo chiamare il metodo dietWithoutResidueOptionWith per ottenere valore enum ...:/
ci sono una soluzione migliore per memorizzare direttamente LocalizedString e non le chiavi in enum?
Grazie
enumerazione
enum DietWithoutResidueOption: String {
case NoDiet = "NoDiet"
case ThreeDays = "ThreeDays"
case FiveDays = "FiveDays"
private func localizedString() -> String {
return NSLocalizedString(self.rawValue, comment: "")
}
static func dietWithoutResidueOptionWith(#localizedString: String) -> DietWithoutResidueOption {
switch localizedString {
case DietWithoutResidueOption.ThreeDays.localizedString():
return DietWithoutResidueOption.ThreeDays
case DietWithoutResidueOption.FiveDays.localizedString():
return DietWithoutResidueOption.FiveDays
default:
return DietWithoutResidueOption.NoDiet
}
}
}
Localizable.strings
"NoDiet" = "NON, JE N'AI PAS DE RÉGIME";
"ThreeDays" = "OUI, SUR 3 JOURS";
"FiveDays" = "OUI, SUR 5 JOURS";
chiamata
println(DietWithoutResidueOption.FiveDays.localizedString())
Non devi dimenticare di aggiungere le versioni localizzate della stringa a "Localizable.strings", altrimenti non ci sarebbe alcuna versione tradotta. – NerdyTherapist