I am working a on a project in swiftui, using swiftdata. I am new and having a hard time understanding how the modelcontext/container works with passing data between views. I have the same error showing up in each preview as well as in my @main App file.
The error reads: Missing argument for parameter ‘activity’ in call Insert ‘, activity: <#Binding#>
I have tried adding in activities and binding but then it usually says it is out of scope. I am probably doing most of this wrong but any help is appreciated.
@Model
final class Activity {
var id: UUID
var title: String
var gear: [Gear]
var theme: Theme
// initialize with default values
init(id: UUID = UUID(), title: String, gear: [String], theme: Theme) {
self.id = id
self.title = title
self.gear = gear.map { Gear(name: $0)}
self.theme = theme
}
}
extension Activity {
struct Gear: Identifiable, Codable {
let id: UUID
var name: String
init(id: UUID = UUID(), name: String) {
self.id = id
self.name = name
}
}
// create an empty activity
static var emptyTrip: Activity {
Activity(title: "", gear: [], theme: .red)
}
}
extension Activity {
static let SampleData: [Activity] = [
Activity(title: "Backpacking",
gear: ["text", "text" text"], theme: .red),
Activity(title: "Climbing",
gear: ["text", "text" text"], theme: .red),
Activity(title: "Day hik",
gear: ["text", "text" text"], theme: .red)
]
}
import SwiftData
import SwiftUI
struct ActivityListView: View {
@Environment(.modelContext) private var modelContext
@Query(sort: Activity.title, order: .forward, animation: .default) private var activities: [Activity]
@Binding var activity: Activity
@State private var isPresentingNewActivityView = false
var body: some View {
NavigationStack {
List {
ForEach(activities) { activity in
NavigationLink {
ActivityDetailView(activity: $activity)
} label: {
CardView(trip: activity)
}
.listRowBackground(activity.theme.mainColor)
.swipeActions {
Button("Delete", role: .destructive) {
modelContext.delete(activity)
}
}
}
}
.navigationTitle("Here's a title")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button(action: {
isPresentingNewActivityView = true
}) {
Image(systemName: "plus")
}
}
}
.overlay {
if activities.isEmpty {
ContentUnavailableView("Some text goes right here." ,
systemImage: "person",
description: Text("Use the '+' to add some stuff."))
}
}
}
.sheet(isPresented: $isPresentingNewActivityView) {
ActivityDetailView(activity: $activity )
}
}
}
#Preview {
ActivityListView()
.modelContainer(for: [Activity.self], inMemory: true)
}