Skip to content

Instantly share code, notes, and snippets.

@Ziewvater
Created November 29, 2016 21:43
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 Ziewvater/27a7a8ed9cda51f98d8b78a5df35354a to your computer and use it in GitHub Desktop.
Save Ziewvater/27a7a8ed9cda51f98d8b78a5df35354a to your computer and use it in GitHub Desktop.
closure property declaration
class SomeView {
// Declare property as a closure that's lazily executed on evaluation
let label: UILabel = {
// Create view within closure
let view = UILabel()
// Do whatever kind of configuration you want on it
view.font = .systemFont(ofSize: 14)
view.textColor = .blue
// return the view at the end of the closure
return view
}() // <-- these parenthesis are important! If you don't include them, you'll get an "accurate" but cryptic compiler error
override init(frame: CGRect) {
super.init(frame: frame)
// You can access the property by name without performing any sort of special initialization or anything
addSubview(label)
/* blah blah do auto layout */
label.text = "This all should work"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment