Skip to content

Instantly share code, notes, and snippets.

@GianlucaGuarini
Created February 3, 2025 15:58
Show Gist options
  • Save GianlucaGuarini/b69b1324c8154daa2f06c8610674c6cc to your computer and use it in GitHub Desktop.
Save GianlucaGuarini/b69b1324c8154daa2f06c8610674c6cc to your computer and use it in GitHub Desktop.
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