2015-01-29 14 views
11

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()) 

risposta

10

È possibile utilizzare qualsiasi tipo StringLiteralConvertible, Equatable per il tipo RawValue di enum.

Allora, che ne dite:

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)) 
    } 
} 

Con questo, è possibile costruire DietWithoutResidueOption da 3 modi:

let option1 = DietWithoutResidueOption.ThreeDays 
let option2 = DietWithoutResidueOption(rawValue: "ThreeDays") // as Optional 
let option3 = DietWithoutResidueOption(localizedString: "OUI, SUR 3 JOURS") // as Optional 

ed estrarre la stringa localizzata con:

let localized = option1.localizedString 
1

Ohter alternativa :

Enum

enum Title : String { 

    case CEO = "CEOKey" 
    case CTO = "CTOKey" 
    case CFO = "CFOKey" 

    private static let allTitles = [CEO, CTO, CFO] 

    var localizedString: String { 
    return NSLocalizedString(self.rawValue, comment: "") 
    } 

    init!(rawValue: String) { 
    var keys = Title.allTitles 
    var filtered = keys.filter { $0.rawValue == rawValue } 

    self = filtered.first! 
    } 

    init!(localizedString: String) { 
    var keys = Title.allTitles 
    var filtered = keys.filter { $0.localizedString == localizedString } 

    self = filtered.first! 
    } 
} 

Localizable.stringhe

"CEOKey" = "Chief Executive Officer"; 
"CTOKey" = "Chief Technical Officer"; 
"CFOKey" = "Chief Financial Officer"; 

enum constract:

let option1 = Title.CFO 
let option2 = Title(rawValue: "CTOKey") // init from key 
let option3 = Title(localizedString: NSLocalizedString("CEOKey", comment: "")) // init from value 

Estrarre le stringhe localizzate:

println("option1 localized string : \(option1.localizedString)") 
println("option2 localized string : \(option2.localizedString)") 
println("option3 localized string : \(option3.localizedString)") 

ingresso

option1 localized string : Chief Financial Officer 
option2 localized string : Chief Technical Officer 
option3 localized string : Chief Executive Officer 

Questo codice genererà un'eccezione, se le stringhe o chiavi localizzate non trovato

4

Prova questa, è abbastanza semplice e diretto:

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() 
    } 
} 

e si potrebbe usare in questo modo:

let stringOfChoice1: String = ChoicesTitle.getTitleFor(title: .choice1) 

Spero che questo funzioni per te

+1

Non devi dimenticare di aggiungere le versioni localizzate della stringa a "Localizable.strings", altrimenti non ci sarebbe alcuna versione tradotta. – NerdyTherapist