Init lazy var using a method and not a block in swift3
In document swift3. It is recommended to use in the lazy
following two ways:
1.directly init
`lazy var someViews: UILabel = UILabel()`
2.init with a block
lazy var overlayView: UILabel = { [unowned self] in
let overlayView = UILabel()
overlayView.backgroundColor = self.backgroundColor.withAlphaComponent(0.90)
overlayView.font = UIFont.boldSystemFont(ofSize: YFCalendarOverlaySize)
overlayView.textColor = self.overlayTextColor
overlayView.alpha = 0.0
overlayView.textAlignment = .center
overlayView.translatesAutoresizingMaskIntoConstraints = false
return overlayView
}()
If I want to lazily initialize some variable with some default value. I can only use the 2nd way. But that seems too awkward. So, I am using the following method to initialize the lazy var. It works fine. But is everything okay? I need help.
class SomeViewController: UIViewController {
lazy var someViews: UILabel = self.initSomeViews()
override func viewDidLoad() {
print(self.someViews)
}
}
fileprivate extension SomeViewController {
func initSomeViews() -> UILabel! {
let overlayView = UILabel()
overlayView.backgroundColor = UIColor.white.withAlphaComponent(0.90)
overlayView.font = UIFont.boldSystemFont(ofSize: YFCalendarOverlaySize)
overlayView.alpha = 0.0
overlayView.textAlignment = .center
overlayView.translatesAutoresizingMaskIntoConstraints = false
return overlayView
}
}
source to share
Yes, that's ok, but yours initSomeViews()
has the same concept of using blocks. You can directly assign him a blacksmith or method to do this.
Note:
If you are using your lazy property in viewDidLoad: then no need to declare it lazy.
-They are initialized only once and never computed again, that is, they are not computed dynamically.
source to share
I advice NOT to use the closure option:
lazy var overlayView: UILabel = { [unowned self] in
let overlayView = UILabel()
// ...
return UILabel()
}()
Why?
I did a little research myself. Follow this link to read the detailed description.
Correct use with a function:
class SomeViewController: UIViewController {
lazy var label: UILabel = self.getLabel()
}
fileprivate extension SomeViewController {
func getLabel() -> UILabel {
return UILabel()
}
}
source to share
For security and style reasons (I'm probably going to take this place ...) I use implicitly expanded options for this:
private var someViews: UILabel!
override func viewDidLoad() {
self.someViews = createSomeViews()
}
private func createSomeViews() -> UILabel { ... }
Security . By doing initialization right away, by method viewDidLoad
, you buy a good path of deterministic code in your controller settings. Conversely, using lazy
, you can have more than one code that triggers the creation of var, potentially hiding hideous hidden bugs (like thinking about cross dependencies in your views, etc.).
Style . What can I say? It just looks better in the eyes :)
But if your var initialization contains expensive computations that you want to delay as much as possible than lazy
- that's the way to go!
source to share