Post

SwiftUI Custom Initializer For Binding Property

1
2
3
4
struct FoodLogQuickAddView: View {
    @Binding var foodLog: FoodLog?
    @Query private var lastNightLogs: [FoodLog]
}

A custom initializer is required for lastNightLogs, which causes FoodLogQuickAddView
to lose the compiler-generated memberwise initializer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct FoodLogQuickAddView: View {
    @Binding var foodLog: FoodLog?
    @Query private var lastNightLogs: [FoodLog]

    init(foodLog: Binding<FoodLog?>) {
        self._foodLog = foodLog
        
        _lastNightLogs = Query(filter: #Predicate<FoodLog> {
            ($0.occurredAt >= yesterdayStart) &&
            ($0.occurredAt < todayStart) &&
            ($0.dayPhase == nightRawValue)
        })
    }
}
This post is licensed under CC BY 4.0 by the author.