Created
February 3, 2025 15:58
-
-
Save GianlucaGuarini/b69b1324c8154daa2f06c8610674c6cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewController: UIViewController { | |
lazy var viewControllers: [UIViewController] = self.lazyControllers.filter { $0 != nil }.map { $0! } | |
private lazy var lazyControllers: LazyMapSequence<[String], UIViewController?> = { | |
let controllerKeys = ["home", "profile", "settings"] | |
return controllerKeys.lazy.map { key in | |
return self.createController(for: key) | |
} | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Accessing viewControllers triggers lazy instantiation | |
print("ViewControllers count: \(viewControllers.count)") | |
} | |
private func createController(for key: String) -> UIViewController? { | |
switch key { | |
case "home": | |
return HomeViewController() | |
case "profile": | |
return ProfileViewController() | |
case "settings": | |
return SettingsViewController() | |
default: | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment