Skip to content

Instantly share code, notes, and snippets.

@mthongvanh
Last active August 29, 2015 14:12
Show Gist options
  • Save mthongvanh/524d71d511943fbf82eb to your computer and use it in GitHub Desktop.
Save mthongvanh/524d71d511943fbf82eb to your computer and use it in GitHub Desktop.
enum PersonDescription {
case Singular, Dual, Plural
struct Portrayal {
var literal: String, numeral: Int
}
var portrayal: Portrayal {
switch self {
case .Singular:
return Portrayal(literal: "Singular", numeral: 1)
case .Dual:
return Portrayal(literal: "Dual", numeral: 2)
default:
return Portrayal(literal: "Plural", numeral: 3)
}
}
var allPersonNumbers: [PersonDescription] {
return [.Singular,.Dual,.Plural]
}
}
func fillNounFields(allDeclensions: [String: AnyObject]) {
func populateFields(declinedForms: [String: AnyObject], number: PersonDescription) {
for wordCase in WordCase.allCases {
var textFieldName = number.description.literal.lowercaseString + wordCase.rawValue.capitalizedString
var textField = self.valueForKey(textFieldName) as? NSTextField
if let textFieldToFill = textField {
if let declinedForm = declinedForms[wordCase.rawValue.lowercaseString] as? String {
textFieldToFill.stringValue = declinedForm
}
}
}
}
for (number,declinedForms) in allDeclensions {
if let declinedNounForms = declinedForms as? [String: AnyObject] {
var person: PersonDescription = { () -> PersonDescription in
var person: PersonDescription = PersonDescription.Singular
switch number {
case PersonDescription.Singular.description.literal:
person = PersonDescription.Singular
case PersonDescription.Dual.description.literal:
person = PersonDescription.Dual
case PersonDescription.Plural.description.literal:
person = PersonDescription.Plural
default:
NSLog("default: \(number)")
}
return person
}()
populateFields(declinedNounForms, person)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment