syphixy
(Syphixy)
1
hello everyone, I'm working on creating my own flashcards app and I found a problem where I'm actually able to fetch the data from CoreData, however, in the closure of ForEach loop, I'm not able to create a stack of flashcards.
Here's the code:
ForEach(flashCardData, id: \.self) { flashcards in
// FlashcardView(flashcard: flashcard)
// Text(flashcards.name ?? "nothing") - have to create a code where it's above
VStack {
Text(flashcards.term ?? "nothing")
.font(.largeTitle)
.bold()
if isShown {
Text(flashcards.definition ?? "nothing")
.font(.largeTitle)
.bold()
}
}
tera
2
Hi, and welcome.
I don't quite understand your question. See this example app, it may help.
Minimal SwiftData application example
import SwiftUI
import SwiftData
@Model final class Card {
var name: String
init(name: String) {
self.name = name
}
}
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query var cards: [Card]
var body: some View {
NavigationView {
List {
ForEach(cards) { card in
Text(card.name)
}
}
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button("Add") {
withAnimation {
let newCard = Card(name: UUID().uuidString)
modelContext.insert(newCard)
}
}
}
}
}
}
}
@main struct CardsApp: App {
var body: some Scene {
WindowGroup { ContentView() }
.modelContainer(for: [Card.self])
}
}