Skip to content

Instantly share code, notes, and snippets.

@yashigani
Created July 27, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashigani/b75c21ebff9f6432d1c1 to your computer and use it in GitHub Desktop.
Save yashigani/b75c21ebff9f6432d1c1 to your computer and use it in GitHub Desktop.
Lazy stored propertyとImplicitly Unwrapped Optionalを組み合わせた時のおもしろい挙動
final class Person {
var name: String
lazy var greeting: ImplicitlyUnwrappedOptional<String> = {
return "Hello, \(self.name)"
}()
init(_ name: String) {
self.name = name
}
}
var p = Person("Steve")
p.name // => steve
p.greeting // => Hello, steve
p.name = "Tim"
p.greeting = nil
p.greeting // => Hello, Tim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment