Junior — Middle
What are the consequences of accessing the view property inside the loadView method in a controller?
sobes.tech AI
Answer from AI
In the loadView method of an iOS controller, the main view (view) is created and assigned. If within loadView you access the view property, it will lead to a recursive call to loadView, because view is not yet initialized and the system will try to load it again, calling loadView once more. This will cause an infinite recursion and, consequently, a stack overflow error.
Therefore, you should not access self.view inside loadView. Instead, you should create and assign a new view directly, for example:
override func loadView() {
let customView = UIView()
customView.backgroundColor = .white
self.view = customView
}