I do have a simple application that uses the new swiftUI framework SwiftData
.
This is the SwiftData
model named Note
:
import Foundation
import SwiftData
@Model
class Note {
init(name: String, content: String) {
self.name = name
self.content = content
}
@Attribute(.unique) var name: String
var content: String
}
And this is the ContentView
sample:
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query var notes: [Note]
@State var newNoteName: String = ""
@State var presentNewNoteAlert = false
var body: some View {
NavigationSplitView {
List {
ForEach (notes){noteTitle in
NavigationLink (noteTitle.name) {
NoteEditing(noteModelClass: noteTitle)
}
}
.onDelete(perform: deleteNote)
}
.navigationTitle("Notes")
.toolbar {
Button ("", systemImage: "plus", action: {
presentNewNoteAlert = true
})
}
} detail: {
Text("Select a note to edit")
}
.alert("New note name", isPresented: $presentNewNoteAlert) {
TextField("", text: $newNoteName)
HStack {
Button("Done", action: submit)
Button("Close", role: .cancel) {}
}
}
}
}
Here, if I used NavigationSplitView
for the ForEach
, there is a weird issue where all NavigationLink
's destinations becomes the same!. But If I change it to NavigationStack
the destination issue fixed.
This is a peak:
NavigationSplitView
duplicating destination issue:
The NavigationStack
behaving good and not duplicating the NavigationLink
's destination: