Member-only story
Lazy NavigationLink for SwiftUI
We don’t want to preload every ViewModel!
4 min readJan 24, 2023
Press enter or click to view image in full size![]()
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.7, and Swift 5.2.4
You want to display some content in a `List`. You place a breakpoint and…well there is a nasty surprise.
Read on!
Prerequisites:
- You will be expected to make a Single View SwiftUI Application] in Swift.
Terminology:
- SwiftUI: A simple way to build user interfaces Across Apple platforms
The motivation
The destination views for navigation links are created when the body for the List is updated — and therefore raises the individual view models.
How did this situation occur?
The code
I’ve rather lazily (on topic for this article!) called the initial view ContentView.swift. Sorry for that.
This means that we create the SwiftUI view and feed it a view model in the SceneDelegate file.
let viewModel = ContentViewModel()
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(viewModel…