我想在本地化字符串中使用枚举,所以我喜欢它,它可以工作,但是这个解决方案的问题是:我无法从本地化字符串中轻松地获得枚举值,我必须有键才能这样做:
let option = DietWithoutResidueOption(rawValue: "NoDiet")如果不是,我必须调用dietWithoutResidueOptionWith方法来获得枚举值.:/
有更好的解决方案可以直接存储localizedString而不是enum中的密钥?
谢谢
枚举
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";调用
println(DietWithoutResidueOption.FiveDays.localizedString())发布于 2015-01-29 11:43:16
对于RawValue类型的enum,可以使用任何enum类型。
那么,不如:
import Foundation
struct LocalizedString: StringLiteralConvertible, Equatable {
let v: String
init(key: String) {
self.v = NSLocalizedString(key, comment: "")
}
init(localized: String) {
self.v = localized
}
init(stringLiteral value:String) {
self.init(key: value)
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(key: value)
}
init(unicodeScalarLiteral value: String) {
self.init(key: value)
}
}
func ==(lhs:LocalizedString, rhs:LocalizedString) -> Bool {
return lhs.v == rhs.v
}
enum DietWithoutResidueOption: LocalizedString {
case NoDiet = "NoDiet"
case ThreeDays = "ThreeDays"
case FiveDays = "FiveDays"
var localizedString: String {
return self.rawValue.v
}
init?(localizedString: String) {
self.init(rawValue: LocalizedString(localized: localizedString))
}
}使用此方法,您可以通过以下三种方式构造DietWithoutResidueOption:
let option1 = DietWithoutResidueOption.ThreeDays
let option2 = DietWithoutResidueOption(rawValue: "ThreeDays") // as Optional
let option3 = DietWithoutResidueOption(localizedString: "OUI, SUR 3 JOURS") // as Optional并使用以下方法提取本地化字符串:
let localized = option1.localizedString发布于 2017-03-23 00:59:21
试试看,它很简单,而且很直接:
enum ChoicesTitle: String {
case choice1 = "Choice 1"
case choice2 = "Choice 2"
case choice3 = "Choice 3"
case choice4 = "Choice 4"
case choice5 = "Choice 5"
case choice6 = "Choice 6"
func localizedString() -> String {
return NSLocalizedString(self.rawValue, comment: "")
}
static func getTitleFor(title: ChoicesTitle) -> String {
return title.localizedString()
}
}你可以这样用它:
let stringOfChoice1: String = ChoicesTitle.getTitleFor(title: .choice1)希望这对你有用
发布于 2019-06-03 00:31:52
这是一个迟来的回答,但我刚刚和苹果工程师聊了一会,他们建议这样做:
enum LocalizedStrings {
case title
var localized: String {
switch self {
case .title:
return NSLocalizedString("My Title", comment: "My Comment")
}
}
}在您的示例中,解决方案与原始代码没有太大的不同:
enum DietWithoutResidueOption {
case NoDiet
case ThreeDays
case FiveDays
var localizedString: String {
switch self {
case .NoDiet:
return NSLocalizedString("NoDiet", comment: "Some comment")
case .ThreeDays:
return NSLocalizedString("ThreeDays", comment: "Some comment")
case .FiveDays:
return NSLocalizedString("FiveDays", comment: "Some 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
}
}
}原因是他们不希望您将变量传递到NSLocalizedString()中。这与优化和解析字符串有关。假设Xcode在某个时候自己生成localizable.strings文件,但是它找不到字符串,因为它们是作为变量传递的。
https://stackoverflow.com/questions/28213693
复制相似问题