RedMak
(Malek)
1
Hi all,
im here to ask a simple question
: is there any downsides of using lazy var like in compilation time or runtime speed .. ?
In my project i really like to have everything related to a var (specially UI components) in one place (layout, label text, colours ..) so i use lazy to create my label (if there's dependencencies between properties, in this case i don't use lazy because it will lead to infinite loop).
here's an exemple:
private lazy var labelDescription: UILabel = {
let label = UILabel(frame: .zero)
label.numberOfLines = 0
label.text = self.computedText()
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
return label
}
Thanks 
1 Like
S2Ler
(Aliaksandr Bialiauski)
3
One downside which I know is that lazy var initialisation isn't atomic. So if you access uninitialised lazy var from multiple threads it might crash the process.
1 Like
RedMak
(Malek)
4
yes im aware about this limitation and this is why i use it mostly for UI component 