If I uncomment self.numberLabel.textColor = UIColor.black
, the build compiles but crashes in the simulator.
lazy public var numberLabel: UILabel = {
self.numberLabel.textColor = UIColor.black
return UILabel(frame: CGRect.init(x: 10, y: 40, width: self.bounds.size.width, height: 20))
}()
The error states: "EXC_BAD_ACCESS".
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
Sample snippet - Swift 3.x
lazy public var numberLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 20, y: 20, width: 200, height: 21))
label.textColor = UIColor.black
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(numberLabel)
numberLabel.text = "Good"
}