TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData Navigation Challenge 1 - Why is this not valid?

Forums > SwiftUI

Great course! This is my solution for Challenge 1 adding delete to the Sights. My code is below for EditDestinationView where I comment out the modelContext property and adjust the deleteSights function. It seems to me that since we're passing in the destination property and declaring it @Bindable that it would bring with it it's reference to the modelContext from DestinationListingView. When I test this, it works as expected, no crashes. Why is this approach not valid? Thank you in advance.

//  EditDestinationView.swift
//  iTour
//
//  Created by Jack Cardinal on 10/18/23.
//

import SwiftUI
import SwiftData

struct EditDestinationView: View {

//    @Environment(\.modelContext) private var modelContext
    @Bindable var destination: Destination
    @State private var newSiteName = ""

    var body: some View {
        Form {
            TextField("Name", text: $destination.name)
            TextField("Details", text: $destination.details, axis: .vertical)
            DatePicker("Date", selection: $destination.date)

            Section("Priority") {
                Picker("Priority", selection: $destination.priority) {
                    Text("Meh").tag(1)
                    Text("Maybe").tag(2)
                    Text("Must").tag(3)
                }
                .pickerStyle(.segmented)
            }

            Section("Sights") {
                ForEach(destination.sights) { sight in
                    Text(sight.name)
                }
                .onDelete(perform: deleteSights)

                HStack {
                    TextField("Add new sight in \(destination.name)", text: $newSiteName)
                    Button("Add", action: addSight)
                }
            }
        }
        .navigationTitle("Edit Destination")
        .navigationBarTitleDisplayMode(.inline)
    }

    func addSight() {
        guard newSiteName.isEmpty == false else { return }

        withAnimation {
            let sight = Sight(name: newSiteName)
            destination.sights.append(sight)
            newSiteName = ""
        }

    }

    func deleteSights(_ indexSet: IndexSet) {
//        for index in indexSet {
//            let sight = destination.sights[index]
//            modelContext.delete(sight)
//        }

        destination.sights.remove(atOffsets: indexSet)
    }
}

#Preview {
    do {
        let config = ModelConfiguration(isStoredInMemoryOnly: true)
        let container = try ModelContainer(for: Destination.self, configurations: config)
        let example = Destination(name: "Example Destination", details: "Example details go here and will automatically expand vertically as they are edited.")

       return EditDestinationView(destination: example)
            .modelContainer(container)

    } catch {
        fatalError("Failed to create model containter.")
    }
}

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.