Lazy Variables Not Supported with @Observable
When migrating to @Observable, lazy variables are not supported. Replace them with computed properties or initialize them directly to ensure compatibility or use @ObservationIgnored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Observable
class WalksViewModel {
let walks = [
Walk(title: "Central Park Loop", difficulty: .easy),
Walk(title: "Mountain Ridge Trail", difficulty: .hard),
Walk(title: "Riverbank Stroll", difficulty: .medium)
]
var sortingIsOn = false
@ObservationIgnored
lazy var sortedWalks: [Walk] = {
walks.sorted(
using: KeyPathComparator(\Walk.difficulty)
)
}()
}