我想要在本地化字符串中使用枚举,所以我是这样做的,它有效,但是这种解决方案的问题是:我无法轻松地从本地化字符串中获取枚举值,我必须拥有该键才能做到:
let option = DietWithoutResidueOption(rawValue: "NoDiet")
如果不是这样,我必须调用dietWithoutResidueOptionWith方法来获取枚举值...:/
是否有更好的解决方案直接存储本地化字符串而不是在枚举中使用键?
谢谢
枚举
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
}
}
}
可本地化字符串
"NoDiet" = "NON, JE N'AI PAS DE RÉGIME";
"ThreeDays" = "OUI, SUR 3 JOURS";
"FiveDays" = "OUI, SUR 5 JOURS";
呼叫
println(DietWithoutResidueOption.FiveDays.localizedString())