Created
November 29, 2016 21:43
-
-
Save Ziewvater/27a7a8ed9cda51f98d8b78a5df35354a to your computer and use it in GitHub Desktop.
closure property declaration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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