Fill the blank
Fill the blank to save a new trip.
@Model final class Trip {
var name: String
var start: Date
init(name: String, start: Date) { self.name = name; self.start = start }
}
struct TripList: View {
@Query(sort: \Trip.start) private var trips: [Trip]
@Environment(\.modelContext) private var context
var body: some View {
List(trips) { Text($0.name) }
.toolbar { Button("Add") { context.____(Trip(name: "Goa", start: .now)) } }
}
}Hint 1 of 1
How does a new object join the context?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A insert
Correct. The context tracks the new model, autosaves it, and @Query updates the list.
Why not the others?
- B
save. save takes no model; it writes pending changes. - C
append. ModelContext isn't an array. - D
add. There is no add method on ModelContext.