Hello to everybody 👋
I and my coworkers can't solve a dispute about initialization of a view hierarchy for a long time. We implement a view hierarchy from code, without using Interface Builder. Each of us has own background experience about how it should work "right". If everything is clearly and understandably in Objective-C, there are some questions in Swift on which Apple hasn't answered.
We have three technical solutions and each of us applies own solution:
1️⃣
class SomeClass: UIViewController {
private var someLabel: UILabel!
private var someSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
let someLabel = UILabel()
someLabel.text = "Test"
view.addSubview(someLabel)
self.someLabel = someLabel
let someSwitch = UISwitch()
someSwitch.isOn = true
view.addSubview(someSwitch)
self.someSwitch = someSwitch
}
}
2️⃣
class SomeClass: UIViewController {
private let someLabel = UILabel()
private let someSwitch = UISwitch()
override func viewDidLoad() {
super.viewDidLoad()
someLabel.text = "Test"
view.addSubview(someLabel)
someSwitch.isOn = true
view.addSubview(someSwitch)
}
}
3️⃣
class SomeClass: UIViewController {
private lazy var someLabel = UILabel()
private lazy var someSwitch = UISwitch()
override func viewDidLoad() {
super.viewDidLoad()
someLabel.text = "Test"
view.addSubview(someLabel)
someSwitch.isOn = true
view.addSubview(someSwitch)
}
}
I suppose that the same question has already discussed somewhere, but I haven't found a discussion here. If you know where there was the discussion, please, send to me.