Skip to content

Instantly share code, notes, and snippets.

@ttepasse
Created February 10, 2015 17:17
Show Gist options
  • Save ttepasse/5cce661a72633cee05a6 to your computer and use it in GitHub Desktop.
Save ttepasse/5cce661a72633cee05a6 to your computer and use it in GitHub Desktop.
enum YesNo {
case Yes, No
}
class Foo {
var bar: YesNo? = .None {
didSet {
self.baz = calculateValueForBaz()
}
}
private func calculateValueForBaz() -> String {
switch (self.bar) {
case .Some(.Yes):
return "Ja"
case .Some(.No):
return "Nein"
default:
return "Möglich"
}
}
lazy var baz: String = {
self.calculateValueForBaz()
}()
}
let foo = Foo()
foo.bar // -> nil
foo.baz // -> "Möglich"
foo.bar = .Yes
foo.baz // -> "Ja"
foo.bar = .No
foo.baz // -> "Nein"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment